Pages

Thursday, February 4, 2010

SharePoint 2007: Hide/Show options from Create page.

When you navigate to Site Settings –> create page you’ll be directed to the create.aspx page as shown below:

image

Figure 1: create page

Now if you want to hide some options in the create page, then how will you do that? Say you don’t want to show the “Document Library” option or you want to hide this option for a particular user or group.

 

Hide option for all users

Say you want to hide “Document Library” option for all users. The following steps will guide you how to do so. But before you proceed with the steps please backup your original create.aspx page.

1. Locate the create.aspx page on your disk: If you take a look at the url of the create page you’ll find it something like “http://server:port/_layouts/create.aspx”. So the create page is from layout folder which is from “Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\template\layouts”. You can open the Create.aspx page in Visual Studio. In the page you’ll find a code block as shown below:

System.Collections.IEnumerator currRg = ((ArrayList)rgRgs[iCat + 5]).GetEnumerator();
while (currRg.MoveNext())
{
                SPListTemplate spListTmpl = (SPListTemplate)currRg.Current;
    SPListTemplateType iTemplateType = spListTmpl.Type;
    if (iTemplateType == SPListTemplateType.InvalidType)
        continue;
 ......................................
}

The above code block actually add create options in the page. So if you modify the code block you can filter the options to show on the page.

 

2. Find the feature id of the create option you want to hide: In order to hide the option you need to know the feature id associated with this create option. To do so move your mouse over the “Document Library” and you’ll find the feature id as shown below:

image

Figure 2: Find feature ID from create page

3. Modify the Create.aspx page to hide option: From step 2 we have found the feature id (of Document Library). Now we need to modify the code in the page to hide the option.

while (currRg.MoveNext())
{
    SPListTemplate spListTmpl = (SPListTemplate)currRg.Current;
    SPListTemplateType iTemplateType = spListTmpl.Type;
    if (iTemplateType == SPListTemplateType.InvalidType)
    continue;
    Guid featidTemplate = spListTmpl.FeatureId;
                
                
    //Added for hiding the "Document Library" option
    if (featidTemplate.ToString() == "00bfea71-e717-4e80-aa17-d0c71b360101")
    continue;
                
                
                
    string strTemplateDisplayName = SPHttpUtility.HtmlEncode(spListTmpl.Name);
    string strTemplateDisplayNameScript = SPHttpUtility.EcmaScriptStringLiteralEncode(spListTmpl.Name);

................................
}

In the above code snippet I have added a condition

if (featidTemplate.ToString() == "00bfea71-e717-4e80-aa17-d0c71b360101") continue;

to check if the featuretemplate id the one for Document Library. If so then I’m continuing to the next iteration rather than adding the option to the page.

 

Hide Options based on User/group

We can hide/show option based on user or group. To check user group we can use SPContext. If you add the following code snippet in place then it’ll show the document library option only for Admins.

if ((featidTemplate.ToString() == "00bfea71-e717-4e80-aa17-d0c71b360101")&&
(!SPContext.Current.Web.CurrentUser.IsSiteAdmin))
continue;

Make sure the hide/show change doesn’t affect other sites

If you make changes to the original create.aspx page then all the sites in that web serve will be affected as the file is shared across all sites. To make sure that only your site gets affected for the changes you need to follow the following steps:

1. Copy the orignal _layouts folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\template\layouts) to some other folder (say C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\template\80_layouts).

2. Go to IIS and expand the site and navigate to the _layouts tree node. Then open the _layouts properties window and change the local path to the place where you have copied folder on step 1(in my case 80_layouts). This is shown below:

image

Figure 3: Change the _layouts virtual directory location

3. Now you can modify the create.aspx page following the steps described on step “hide option for all users” or “hide options based on user/group”.

3 comments:

  1. Really good article - helped me out a bunch!

    ReplyDelete
  2. Is this possible for the Sites and Workspaces link since there doesn't seem to be a Feature ID associated with it when hovering over the link?

    ReplyDelete
  3. The Sites and Workspaces link is not managed in the loop I specified in the blog. In the create.aspx page there's a section in the condition 'if(Web.DoesUserHavePermissions(SPBasePermissions.ManageSubwebs))'. Hiding this section will not render 'create sites and workspaces' option in the output page.

    ReplyDelete

Note: Only a member of this blog may post a comment.