Friday, February 12, 2010

SharePoint 2007: Create web part page library, add a webpart page with webpart without using publishing feature

You can create a document library with several document templates for example, excel, word, webpart page etc. If you need to create a document library with ‘web part page’ document template programmatically then you can do it without enabling publishing feature. Most of the examples on the web uses publishing feature.

Create a new document library with ‘web part page’ document template

The following code snippet shows how to Create the web part page document library.

private void CreateWebPartPageLibrary(SPWeb web)
{
//get the document library list template
var documentListTemplate = web.ListTemplates["Document Library"];
//get document template (word file,excel file, web part pages etc) that will be used in the documentlibrary
var webPartPageDocTempalte = GetWebPartPageDocTemplate(web);
web.Lists.Add(PAGE_LIBRARY_NAME, "Programmatically added library", documentListTemplate, webPartPageDocTempalte);

}
private SPDocTemplate GetWebPartPageDocTemplate(SPWeb web)
{
SPDocTemplate docTemplate = null;
foreach (SPDocTemplate template in web.DocTemplates)
{
if (template.Name.Equals("Web Part Page", StringComparison.OrdinalIgnoreCase))
{
docTemplate = template;
break;
}
}
return docTemplate;
}

In the CreateWebPartPageLibrary method, the first line get the template for document library. Then the GetWebPartPageDocTemplate return the document template for “web part page”. Finally I have called web.lists.add method to add our new list to the list collection.

 

Add a web part page in Web Part page library

Once you have web part page library, you need to add web part page in the library. You can do so programmatically. To add the page you need to prepare an xml command and execute with with ProcessBatchData method.

SPList list = web.Lists["MyPagesLibrary"];
string postInformation =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Method>" +
"<SetList Scope=\"Request\">" + list.ID + "</SetList>" +
"<SetVar Name=\"ID\">New</SetVar>" +
"<SetVar Name=\"Cmd\">NewWebPage</SetVar>" +
"<SetVar Name=\"Type\">WebPartPage</SetVar>" +
"<SetVar Name=\"WebPartPageTemplate\">1</SetVar>" +
"<SetVar Name=\"Title\">{0}</SetVar>"+
"<SetVar Name=\"Overwrite\">true</SetVar>" +
"</Method>";

web.ProcessBatchData(postInformation);

In the above code snippet a command is prepared to generate a web part page. In the command the Type is WebPartPage. WebPartPageTemplate is for different page layout that we can find in the sharepoint UI. The page title is only title without aspx.

Add web part to webpart page

You have created a ‘web part page’ library and added a webpart page. Now you need to add your web part in that page. First of all you need to create a web part instance. The following code Snippet shows how to create a web part programmatically.

private System.Web.UI.WebControls.WebParts.WebPart GetWebPart(SPWeb web, string webPartName)
{
var query = new SPQuery();
query.Query = String.Format(
"<Where><Eq><FieldRef Name='FileLeafRef'/><Value Type='File'>{0}</Value></Eq></Where>",
webPartName);

SPList webPartGallery;
if (web.IsRootWeb)
{
webPartGallery = web.GetCatalog(
SPListTemplateType.WebPartCatalog);
}
else
{
webPartGallery = web.ParentWeb.GetCatalog(
SPListTemplateType.WebPartCatalog);
}
var webParts = webPartGallery.GetItems(query);
var typeName = webParts[0].GetFormattedValue("WebPartTypeName");
var assemblyName = webParts[0].GetFormattedValue("WebPartAssembly");
var webPartHandle = Activator.CreateInstance(
assemblyName, typeName);

System.Web.UI.WebControls.WebParts.WebPart webPart =
(System.Web.UI.WebControls.WebParts.WebPart)webPartHandle.Unwrap();
return webPart;
}

The web part name in the above method GetWebPart() is like “employeeviewer.webpart”. The webpart collection exists in the rootweb. So I have check the IsRootWeb to make sure I run the caml query against the root web.

Once you have got the web part instance created by Activator.CreateInstance you can add it in the page.

const string WEBPART_TITLE="test web part"
//get the web part with GetWebPart method that I have posted in this blog
System.Web.UI.WebControls.WebParts.WebPart webPart = GetWebPart(web, "wpCopyFile.webpart");
using (webPart)
{
using (SPLimitedWebPartManager manager = web.GetLimitedWebPartManager(string.Format("Pages/{0}.aspx",WEBPART_PAGE_NAME), PersonalizationScope.Shared))
{
//if web part already exists then return
foreach (System.Web.UI.WebControls.WebParts.WebPart oldWebPart in manager.WebParts)
{
if (oldWebPart.Title.Equals(WEBPART_TITLE))
return;
}
webPart.Title = WEBPART_TITLE;
manager.AddWebPart(webPart, "", 0);
manager.SaveChanges(webPart);
}
}


The code snippet above call the GetWebPart() method to get the instance of the web part. Then using LimitedWebPartManager it get the page’s web part manager. Then it check if the web par already exists in the page by checking title. And finally add the web part if it doesn’t exists. FYI, be careful to dispose webpart and SPLimtiedWebPartManager.

1 comment: