Friday, December 9, 2011

Customize Blog Posts in SharePoint 2010 Blog Site Template


If you are using the out of the box SharePoint blog site and you want to customize the layout of the blog posts, this is how you do it.  This requires you to create a new XSL template file and then connect that file to the blog post.

This example shows how we remove “By” section of the blog post shown in the figure below.



1. Locate the blog.xsl file on your SharePoint server. It is located in:
          C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\XSL

2. Get a copy of it and rename the copy. (Ex: CustomBlog.xsl). Make sure CustomBlog.xsl is saved in the same directory as blog.xsl.

3. Modify CustomBlog.xsl. You have to find respective statement for the by line and remove it. The statement of my file was:
  <xsl:when test="@Name='Author'">
    <span class="ms-postfootercolor">
      <xsl:value-of select="$thisNode/../@resource.wss.ByPrefix"/>
    span>
    <xsl:text disable-output-escaping="yes" ddwrt:nbsp-preserve="yes">&nbsp;xsl:text>
    <xsl:value-of select="$thisNode/@Author.span" disable-output-escaping="yes" />
  xsl:when>


4. Go to the Blog home page (http://<blog>/default.aspx), then Site Actions –> Edit Page.
Select Edit Web Part option of the Posts Web Part.



5. Expand “Miscellaneous” section of the edit web part properties window. Specify “/_layouts/xsl/CustomBlog.xsl” as the XSL Link. Save properties.


 
6. Save changes and refresh the page. You will find “by” section is removed from the blog post as shown in the below figure.


Saturday, November 5, 2011

Find all References to a Master Page or Page Layout in SharePoint

“This item cannot be deleted because it is still referenced by other pages” is a common error we get when we trying to delete a master page or page layout from the “Master pages and page layouts” gallery.
      

One of the ways to find all the references to a file in SharePoint is to use the "Site Content and Structure" page from the Site Actions menu, which is used to manage both the content and structure of the SharePoint site collection.

1. Go to “Site Actions” -> “Site Content and Structure” and select “Master Page Gallery”. 

2. Click on "Show Related Resources" on the toolbar.

3. Select the file you want to find all the references. All the related resources will be listed down with the location.

In addition to find file references, the “Site Content and Structure” page is a great way to copy or move files between libraries on different sites in a site collection. More information on how to work with Site Content and Structure can be found here.


If you are interested in getting all related resources of a file programmatically, we can do it by using BackwardLinks property and ForwardLinks property of SPListItem object.

using (SPSite site = new SPSite("http://prasad"))
{
   using (SPWeb web = site.OpenWeb())
   {
      // Get default.aspx
      SPFile file = web.GetFile("default.aspx");

      // Gets the URLs of all files that link to the current file from the current site collection. Pages that link to the current file from another Web site or site collection are not included in the backward link information.
      foreach (SPLink link in file.BackwardLinks)
      {
         Console.WriteLine(link.Url);
      }

      // Gets the URLs of all the links from the file to other pages, including internal links (to pages in the same Web site), and external links (to pages in other Web sites and site collections on the server).
      foreach (SPLink link in file.ForwardLinks)
      {
         Console.WriteLine(link.Url);
      }
   }
}

This code is not only applicable to Master pages and Page Layouts, but also applicable to Web Parts pages, items in a document libraries, and files in folders, as SPFile class represents a file in a SharePoint Web site that can be a Web Parts page, an item in a document library, or a file in a folder.

Friday, November 4, 2011

How to Embed Managed Metadata into Publishing Page Layout

Managed metadata is a hierarchical collection of managed terms that we can define, and then use as attributes for items. This post details how to add these types of fields to a publishing page layout so that content authors can add metadata and keywords to webpages they create using these layouts.
First we need to have a content type created which have added a site column of Managed Metadata type.  

Step 1: We have to register Microsoft.SharePoint.Taxonomy namespace in the page layout.

<%@ Register Tagprefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


Step 2: We can use TaxonomyFieldControl class to add managed metadata field to the page layout. TaxonomyFieldControl provides the edit experience for a TaxonomyField object.

<Taxonomy:TaxonomyFieldControl ID="metadataPlaneTreeTaxonomy" FieldName="Plane Tree Taxonomy" runat="server">Taxonomy:TaxonomyFieldControl>


The taxonomy field control contains a taxonomy Web tagging control, which is responsible for initializing a new instance of the Web tagging control with all of the properties for the TaxonomyField object.

Saturday, October 1, 2011

Specifying Return URL/ Source URL for SharePoint Forms

By default, when users fill out a SharePoint form, they will be navigated back to the "All Items" page after pressing OK. For example if I’m in the “Add new item” form (http://prasad/Lists/MyCustomList/NewForm.aspx) or “Edit item” form (http://prasad/Lists/MyCustomList/EditForm.aspx?ID=7) of the custom list called “MyCustomList”, I will be taken back to “All Items” view (http://prasad/Lists/MyShortCutsList/AllItems.aspx) of the custom List, once I press “Save” or “Cancel” button.

We can change this behavior by specifying a ReturnURL in the form URL by providing a value to “source” querystring. “Source” query string supports both relative URL and the full URL.

Example:
http://prasad/Lists/MyCustomList/NewForm.aspx ?Source=http://prasad/Pages/default.aspx
http://prasad/Lists/MyCustomList/NewForm.aspx ?Source=/Pages/default.aspx

Specifying source querystring as above will be taken you to the home page, when you use “Add new item” form.

Tuesday, September 6, 2011

Customize SharePoint 2010 Global Navigation

The goal of this post is to modify the global navigation of a SharePoint site by using an Event Receiver.
Following Event Receiver will add two new links to the global navigation as the last items of the navigation. New links to be added are picked from the Feature Template file. Here links are separated by “;#” character sequence and display name and the navigation URL are separated by a semicolon. Here I have used SPWeb.Navigation Property, which returns an object that represents navigation on the website, including the Quick Launch area and the top navigation bar.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
string siteURL = ((SPWeb)properties.Feature.Parent).Site.Url;
string newLinksString = string.Empty;
foreach (SPFeatureProperty featureProperty in properties.Feature.Properties)
{
if (featureProperty.Name.Equals("NewLinks"))
{
newLinksString = featureProperty.Value;
}
}
string[] newLinks = newLinksString.Split(new string[] { ";#" },
StringSplitOptions.RemoveEmptyEntries);
using (SPWeb currentWeb = (SPWeb)properties.Feature.Parent)
{
using (SPSite site = currentWeb.Site)
{
using (SPWeb web = site.OpenWeb())
{
SPNavigationNodeCollection globalNavNodes =
web.Navigation.TopNavigationBar;
if (globalNavNodes != null)
{
foreach (string item in newLinks)
{
string[] newLink = item.Split(new string[] { ";" },
StringSplitOptions.RemoveEmptyEntries);
SPNavigationNode newMenuItem =
new SPNavigationNode(newLink[0], newLink[1], true);
globalNavNodes.AddAsLast(newMenuItem);
}
}
web.Update();
}
}
}
}

Following Event Receiver will remove links from the global navigation, which are specified in the Feature Template file in “DeleteLinks” property. Again the links are separated by “;#” character sequence. The value specified for the “DeleteLinks” property is the URL of a site-site called “GroupWork”. Here I have used PublishingWeb.Navigation Property, which returns a PortalNavigation object that represents the navigation settings for the current publishing Web.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
string siteURL = ((SPWeb)properties.Feature.Parent).Site.Url;
string linksStringToRemove = string.Empty;
foreach (SPFeatureProperty featureProperty in properties.Feature.Properties)
{
if (featureProperty.Name.Equals("DeleteLinks"))
{
linksStringToRemove = featureProperty.Value;
}
}
string[] linksToRemove = linksStringToRemove.Split(new string[] { ";#" },
StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < linksToRemove.Length; i++)
{
siteURL = string.Concat(siteURL.TrimEnd('/'), "/",
linksToRemove[i].TrimStart('/'));
using (SPSite site = new SPSite(siteURL))
{
using (SPWeb rootWeb = site.RootWeb)
{
using (SPWeb web = site.OpenWeb())
{
if (PublishingWeb.IsPublishingWeb(rootWeb))
{
PublishingWeb publishingWeb =
PublishingWeb.GetPublishingWeb(rootWeb);
publishingWeb.Navigation.ExcludeFromNavigation(true, web.ID);
publishingWeb.Update();
}
}
}
}
}
}

My Feature Template file contains two properties specifying what the links to be added are and what are the links to be removed. Here links are separated by “;#” character sequence.

Feature.Template.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
<Properties>
<Property Key="DeleteLinks" Value="/GroupWork"/>
<Property Key="NewLinks" Value="WebMail;https://webmail.abcde.org/owa;#
SharePoint eLearning;
http://www.microsoft.com/learning/en/us/training/sharepoint.aspx"
/>
</Properties>
</Feature>

Note: Publishing features needs to be activated in order to use the Event Receiver for removing links. Also it will only work if the feature is activated after creating the site. It won’t work as expected, if we try to activate it by onet.xml as still the site structure is being created.

Friday, August 26, 2011

SharePoint 2010 PortalNavigation Class

Customizing global and left navigation of SharePoint sites is a common task. But some of the properties worked in SharePoint 2007 are now deprecated and no longer working. As a solution SharePoint 2010 PortalNavigation Class comes in handy.
SharePoint 2010 PortalNavigation Class represents navigation for portal pages and other portal navigation objects. The PortalNavigation type exposes the following members and all of them are Public properties.

  • AutomaticSortingMethod: Controls the property to use when automatically sorting navigation items owned by this site.
  • CurrentDynamicChildLimit: Controls the maximum number of "dynamic" child items to show beneath this site in current (left) navigation.
  • CurrentIncludePages: Controls whether publishing pages in this site will be automatically included in current navigation.
  • CurrentIncludeSubSites: Controls whether sub-sites of this site will be automatically included in its current navigation.
  • CurrentNavigationNodes: A collection of SPNavigationNode objects that get included in the current navigation of the associated site.
  • GlobalDynamicChildLimit: Controls the maximum number of "dynamic" child items to show beneath this site in global navigation.Dynamic children are sub-sites (represented by SPWeb objects) and publishing pages. The default value is 20. Setting the value to 0 removes the limit.
  • GlobalIncludePages: Controls whether publishing pages in this site will be automatically included in global navigation.
  • GlobalIncludeSubSites: Controls whether sub-sites of this site will be automatically included in its global navigation.
  • GlobalNavigationNodes: A collection of SPNavigationNode objects that get included in the global navigation of the associated site.
  • InheritCurrent: Controls whether the associated site should display its own current navigation or should use the current navigation of its parent.
  • InheritGlobal: Controls whether the associated site should display its own global navigation or whether it should use the global navigation of its parent.
  • OrderingMethod: Controls the ordering of navigation items owned by this site.
  • ShowSiblings: Controls whether this site should display its sibling items in the current navigation.
  • SortAscending: Sorts items in a portal navigation in ascending alphanumeric order.

This is how we use these properties in Site definitions. To control the navigation settings of the site you need to activate the ‘NavigationProperties’ feature and specify the properties that you want to apply to the site.

<WebFeatures>

...
...
<!–- Navigation properties for global and current navigation -–>
<Feature ID="541F5F57-C847-4e16-B59A-B31E90E6F9EA">
<Properties xmlns="http://schemas.microsoft.com/sharepoint/">
<Property Key="InheritGlobalNavigation" value="false" />
<Property Key="InheritCurrentNavigation" value="false" />
<Property Key="GlobalIncludeSubSites" value="true" />
<Property Key="GlobalIncludePages" value="true" />
<Property Key="CurrentIncludeSubSites" value="false" />
<Property Key="CurrentIncludePages" value="false" />
<Property Key="IncludeInGlobalNavigation" value="true" />
<Property Key="IncludeInCurrentNavigation" value="true" />
</Properties>
</Feature>
</WebFeatures>

PortalNavigation Class description and a code example to configures portal navigation for a specified site is available on MSDN here.