Pages

Search This Blog

Showing posts with label Master Page. Show all posts
Showing posts with label Master Page. Show all posts

Thursday, July 23, 2015

Sharepoint hide an item from edit control block (ECB)

A simpler way to remove any item from the Edit Control Block of SharePoint 2013 site is to add some javascript in master page. This way we can also hide the item(s) from specific lists. To do this, just add the below code in a javascript file and link that js file to your master page. Alternatively you can also make use of Content Editor Web part to embed it in a page.


$(document).ready(function ()
{
    $('.ms-core-menu-list').live('focus', function ()
    {
        var ecbToHide = "Version History";
        var showVersionHistory = $("li[text='" + ecbToHide + "']");
        if window.location.href.toLowerCase().indexOf(encodeURIComponent('/Lists/TechPerspect/')) != -1)
        {
            showVersionHistory.hide();
        }
        else
        {
            showVersionHistory.show();
        }
    });

});

The above snippet will remove the ECB item - "Version History" from the List - "TechPerspect". 

Tuesday, December 17, 2013

Programmatically adding JQuery, custom JS and CSS file reference to your master page without modifying the master page

I found very interesting component of SharePoint -AdditionalPageHead Content Place Holder in master page which can be used for adding JavaScript, Meta Tags, CSS Styles or other content to the page without modifying them.

One practical scenario would be programmatically adding JQuery, custom JS and CSS file reference to your master page without modifying the master page it.

You need to create a Delegate Control and deploy it using a feature. Below is the snapshot for Feature.XML file.
<?xml version="1.0" encoding="utf-8"?>
  <Control Id="AdditionalPageHead"
           ControlSrc="~/_controltemplates/MyFolder/MyCustomPageHead.ascx"
           Sequence="1499"/>
</Elements>

Change the sequence number and control path and name as per your requirement. 

In the MyCustomPageHead.ascx add the references of JS/CSS

<script type="text/javascript" src="/_layouts/../JS/jquery-1.7.1.min.js"></script>
….
….
<link rel="stylesheet" type="text/css" href="/_layouts/../../../jquery-ui-1.8.18.custom.css" />
<script type="text/javascript">
</script>


Just deploy your feature and activate it in the site where you want to add JS and CSS references.

Wednesday, January 25, 2012

How to set Favicon to the SharePoint 2010 site

SharePoint 2010 comes with a orange Favicon and the image is present in following location
SharePoint Root Folder\Template\Images
[eg. C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\IMAGES\].

Open this folder and search for favicon.ico and rename the file to FaviconBackup.ico.
Now copy your favicon to this folder and make sure it's named as "favicon.ico".
Now restart IIS(open Command Prompt -> IISReset), clear browser cache/temproary internet files.
Close the browser and reopen then browse the SharePoint site.

If mulitple sites are hosted in same farm and need to set different favicons for different sites then we have to update Master Page Code.
Open the site with SharePoint Designer 2010 and select Master pages -> v4.master. The default.master is still present in SharePoint 2010 for backward compatibility and visual upgrade.
But SharePoint 2010 uses v4.master only. Now click "Edit File" link to add the favicon code. If it asks for check out, click yes and continue.

Find the following line in the master page:


<SharePoint:SPShortcutIcon runat="server" IconUrl="/_layouts/images/favicon.ico" />


and change the path of favicon.
e.g.:
<SharePoint:SPShortcutIcon runat="server" IconUrl="/_layouts/images/brickred/favicon.ico" />


Save and close (If you have checked out then you need to check in and site collection administrator has to approve it via Site Settings -> Galleries -> Master pages and page layouts -> Check in and Approve).

Now reset IIS/clear browser cache and check.


Monday, April 18, 2011

Page Title disappears after Ajax Post back

The Problem:
SharePoint page titles disappears when Ajax postback, title turn to square icons instead of page title.

We are having custom form and are using ajax to save the postback but when page postback asysnc the page title turns to square.

Solution :

This was the line in master page after master page design:

<title id=”onetidTitle”>
<asp:ContentPlaceHolder id=”PlaceHolderPageTitle” runat=”server”/>
</title>


After figuring out i found that if i removed the space between the html tag then problem get resolved and title appears as it required.

The correct line of code is as follows :

<title id=”onetidTitle”><asp:ContentPlaceHolder id=”PlaceHolderPageTitle” runat=”server”/></title>

Wednesday, April 6, 2011

Adding and Validating SharePoint InputFormTextBox Control

Recently, I have faced an issue while validating "InputFormTextBox / Rich Text Box" client side.

"InputFormTextBox" is an sharepoint control. To use this control in your custom page following steps must be followed:

1. Add the required directive at the top of the ascx page (if you're using a web user control):
<%@ Register TagPrefix="SharePoint Namespace="Microsoft.SharePoint.WebControls"Assembly="Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>

2. For creating instance of SharePoint Rich Text Box (InputFormTextBox) control in your ascx (if you're using a web user control):

<SharePoint:InputFormTextBox ValidationGroup="UserRegistrationGroup" runat="server" ID="MessageBody" Rows="15" Columns="40" RichText="true" RichTextMode="FullHtml" AllowHyperlink="true" TextMode="MultiLine" CausesValidation="true" />


To validate this control:

3. Add Custom Validator



<asp:CustomValidator CssClass="requirefieldtxt" ID="CustomValidator1" ClientValidationFunction="ValidateMessageBody" runat="server" ValidationGroup="UserRegistrationGroup" ControlToValidate="MessageBody" Display="Dynamic" ErrorMessage="Please input valid message."></asp:CustomValidator>


Custom Validator Code:

<script language="javascript" type="text/javascript">
function ValidateMessageBody(source, args)

{

try

{
//Create Rich TextBox Editor control object

var docEditor = RTE_GetEditorDocument(source.controltovalidate);

if (null == docEditor)

return;

var strHtml = docEditor.body.innerText;

if(strHtml == "")

{

args.IsValid = false;

return;

}

} catch (err) {}

args.IsValid = true;


}


</script>

This will resolve the validation issue.



Friday, February 11, 2011

Publishing Sharepoint 2007 Publishing Pages,Master pages using Object Model

Requirement : We are having 150+ pages. So every time we have to demo when we have to go to pages library and master page library to check the publishing status. It was tedious task to make sure that all the pages, master pages and css file in style library are check in and published.
Solution : We have create a console application so every time when we needs all the pages to be check in and approve all we have to do is to run the console applicaton .
Code:


using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
namespace Publishing_Sharepoint_Pages
{
class Program
{
static void Main(string[] args)
{
//Get the reference of site object
using (SPSite site = new SPSite("http://test"))
{
//Get the spweb object
using (SPWeb webs = site.OpenWeb())
{
// To publish all the page in pages library
//Get the publishing instance of the web
PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(webs);
//Get the pages library
PublishingPageCollection pages = pubWeb.GetPublishingPages();
foreach (PublishingPage page in pages)
{
SPModerationInformation moderationStatus = page.ListItem.ModerationInformation;
//Check if the page is not approved
if (moderationStatus.Status != SPModerationStatusType.Approved)
{
// the page page status approved and update the page status
page.ListItem.ModerationInformation.Status = SPModerationStatusType.Approved;
page.Update();
}
}
// Code to publish the master page
//Get the master page libaray reference
SPList masterPageGallery = site.GetCatalog(SPListTemplateType.MasterPageCatalog);
//Navigate the all item in master page
// This will also include all the page layouts
foreach (SPListItem galleryPage in masterPageGallery.Items)
{
//if the page is status in not published
if (!galleryPage.HasPublishedVersion)
{
//Check in the page with automated comments and set the status to checkin
galleryPage.File.CheckIn("Automatically approved", SPCheckinType.MajorCheckIn);
galleryPage.File.Update();
// approve the page with comments
galleryPage.File.Approve("Automatically approved");
galleryPage.File.Update();
}
}
// Update the style library items
SPList styleLibrary = webs.Lists["Style Library"];
//Get all the css from the style library
SPFolder editingMenuFolder = styleLibrary.RootFolder;
foreach (SPFile cssFile in editingMenuFolder.Files)
{
// if the file is not checkin then check in the file with comments.
if (cssFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
cssFile.CheckIn("Automatically approved");
cssFile.Update();
}
}
}
}
}
}
}