Tuesday, July 3, 2012

Modify the table of content webpart property programatically in sharepoint


In this post i mentioned how to access the Table of content webpart progrmatically and how to modify its property.

  1. So for that first open the visual studio 2010.
  2. Then create one Empty sharepoint project.
  3. specify your site url on which the solution need to deploy.
  4. Then add sharepoint map folder by right click on the project and click on Add -> Sharepoint Mapped Folder - > Select Template - > ControlTemplate - Click OK .
  5. Add one  empty folder and named it as TOCWebpart inside the ControlTemplate folder.
  6. Add user control inside the TOCWebpart folder.

Below is my user control code.


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.Publishing.WebControls;
using SharePoint.CMS.Common;


namespace SharePoint.CMS.SP.UserControls
{
public partial class TOCWebpartModification : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (SPContext.Current.ListItem != null && SPContext.Current.ListItem.File != null)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPFile page = SPContext.Current.ListItem.File;
if (page.Level == SPFileLevel.Checkout && (page.Item.ContentType.Name == "SharePoint News Page"))
{
bool allowUnSafeUpdates = SPContext.Current.Web.AllowUnsafeUpdates;
SPContext.Current.Web.AllowUnsafeUpdates = true;
using (SPLimitedWebPartManager wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
{
try
{
string webpartZoneID = "TOCWebpartZoneID";
for (int i = 0; i < wpm.WebParts.Count; i++)
{
if (wpm.GetZoneID(wpm.WebParts[i]) == webpartZoneID)
{
TableOfContentsWebPart tocWP = wpm.WebParts[i].WebBrowsableObject as TableOfContentsWebPart;
string tocTitle = Convert.ToString(tocWP.Title).ToLower();
tocWP.LevelsToShow = 2;
tocWP.IncludeContentFromStartingLocation = true;
tocWP.AnchorLocation = Convert.ToString(Helper.GetResourceStringForCulture(SPContext.Current.Web.Locale, "AboutSharePoint_Site__RelativeURL"));
wpm.MoveWebPart(tocWP, webpartZoneID, 0);//This line will change the webpart zone index to zero
wpm.SaveChanges(wpm.WebParts[i]);
}
}
}
catch (Exception ex)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Logger.Log(ex);
});
}
finally
{
wpm.Web.Dispose();
}
}
SPContext.Current.Web.AllowUnsafeUpdates = allowUnSafeUpdates;
}
});
}
}
catch (Exception ex)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
Logger.Log(ex);
});
}
}
}
}


Deploy the solution.


Now we are going to add this user control reference to the page to use this webpart.


<%@ Register TagPrefix="TOC" TagName="SharepointTOC" Src="~/_controltemplates/TOCWebpart/TOCWebpartModification.ascx" %>


Now put this usercontrol  reference to the page by using 


<TOC:SharepointTOC ID="ucSharepointTOC" runat="server" ></TOC:SharepointTOC>


Regards
Hiren Patel

No comments:

Post a Comment