Friday, December 10, 2010

Creating webpart instance from assembly name and adding it into a webpart zone- SharePoint

We had an requirement to replace an existing webpart with a new webpart from a webpart zone on a publishing page in sharepoint portal. To accomplish this in a generic way we decided to create an utility which will use the new and old webpart DLL name. The DLL name will be passed as string. The utility will delete the existing webpart and create an instance of the webpart to be added from the DLL name.

We used Assembly class to create an instance of the webpart.
Here is the code-

using System.Reflection;

private static void GetWebsRecursively(SPWeb web, string pageName, string RemovedWebPartDll, string RemovedWebParttitle, string newWebPart, string IsPageOrLayout,string newWebPartTitle)
{
//pageName= name of the page/layout from where webpar has to be replaced
//RemoveWebPartDll= name of the DLL which has to be removed
//RemovedWebParttitle= title of the webpart to be removed
//newWebPart= DLL name of the webpart to be added
//IsPagOrLayout= value to indicate whether the page is publishing page
// or a layout. value would either be page or layout.
SPFile objFile = null;
string strCheckInMsg = "";
try
{
AppDomain domain = AppDomain.CurrentDomain;
AppDomain newDomain = AppDomain.CreateDomain("wpDomain");
DirectoryInfo dirInfo = new DirectoryInfo(@"C:\WINNT\assembly\gac_msil\" + newWebPart.Substring(0, newWebPart.LastIndexOf('.')));
DirectoryInfo[] dir = dirInfo.GetDirectories();
FileInfo[] file = dir[0].GetFiles();
string filePath = file[0].FullName;
Assembly asm = newDomain.Load(File.ReadAllBytes(filePath));
System.Web.UI.WebControls.WebParts.WebPart newWP = null;
newWP = (System.Web.UI.WebControls.WebParts.WebPart)asm.CreateInstance(newWebPart);
strCheckInMsg = "Modified by webpart delete console app";
if (PublishingWeb.IsPublishingWeb(web))
{
PublishingWeb pWeb = PublishingWeb.GetPublishingWeb(web);
if (IsPageOrLayout.ToUpper() == "PAGE")
{
string path = web.Url.ToString() + "/Pages/" + pageName;
objFile = web.GetFile(path);
}
else
{
string path = web.Url.ToString() + "/_catalogs/masterpage/" + pageName;
objFile = web.GetFile(path);
}
}

// If file is already in checked out state then first checkin that and approve
if (objFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
{
objFile.CheckIn(strCheckInMsg);
objFile.Publish(strCheckInMsg);
objFile.Approve(strCheckInMsg);

}


if (objFile.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
objFile.CheckOut();

if (HttpContext.Current == null)
{
HttpRequest request = new HttpRequest("", web.Url, "");
HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter()));
HttpContext.Current.Items["HttpHandlerSPWeb"] = web;
}

SPLimitedWebPartManager wpMgr = SPContext.Current.Web.GetLimitedWebPartManager(objFile.Url, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
for (int i = 0; i < wpMgr.WebParts.Count; i++)
{
if (wpMgr.WebParts[i].GetType().FullName.ToUpper() == RemovedWebPartDll.ToUpper() && wpMgr.WebParts[i].Title.ToUpper() == RemovedWebParttitle.ToUpper())
{
string s = wpMgr.GetZoneID(wpMgr.WebParts[i]);
wpMgr.DeleteWebPart(wpMgr.WebParts[i] as System.Web.UI.WebControls.WebParts.WebPart);
newWP.Title = newWebPartTitle;
wpMgr.AddWebPart(newWP, s, 1);
web.Update();
}
}
objFile.CheckIn(strCheckInMsg);
objFile.Publish(strCheckInMsg);
objFile.Approve(strCheckInMsg);
}

}
catch (Exception ex)
{
Console.WriteLine("Error Occoured Message : " + ex.InnerException);
}
}

This code will replace the existing webpart from new webpart provided the new webpart is present in the GAC.
sounds very good :)

No comments:

Post a Comment