Thursday, July 7, 2011

Adding a Web Part to a page

using System;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;

namespace SampleCode
{
class AddWebPart
{
static void Main()
{
ClientContext oClientContext = new ClientContext("http://MyServer/sites/MySiteCollection");
File oFile = oClientContext.Web.GetFileByServerRelativeUrl("Default.aspx");
LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

string xmlWebPart = "" +
" " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"" +
" xmlns=\"http://schemas.microsoft.com/WebPart/v2\">" +
"My Web PartDefault" +
"Use for formatted text, tables, and images." +
"true0" +
"Normaltrue" +
"truetrue" +
"truetrue" +
"truetrue" +
"ModelessDefault" +
"Cannot import this Web Part." +
"/_layouts/images/mscontl.gif" +
"Microsoft.SharePoint, Version=13.0.0.0, Culture=neutral, " +
"PublicKeyToken=94de0004b6e3fcc5
" +
"Microsoft.SharePoint.WebPartPages.ContentEditorWebPart" +
"" +
"" +
" 
And this is a second paragraph.]]>" +
"";

WebPartDefinition oWebPartDefinition = limitedWebPartManager.ImportWebPart(xmlWebPart);

limitedWebPartManager.AddWebPart(oWebPartDefinition.WebPart, "Left", 1);

oClientContext.ExecuteQuery();
}
}
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...