Showing posts with label update webpart title using client context. Show all posts
Showing posts with label update webpart title using client context. Show all posts

Thursday, July 7, 2011

Deleting a Web Part from a page

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

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

oClientContext.Load(limitedWebPartManager.WebParts);

oClientContext.ExecuteQuery();

if (limitedWebPartManager.WebParts.Count == 0)
{
throw new Exception("No Web Parts to delete.");
}

WebPartDefinition webPartDefinition = limitedWebPartManager.WebParts[0];

webPartDefinition.DeleteWebPart();

oClientContext.ExecuteQuery();
}
}
}

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();
}
}
}

Updating the title of a Web Part

using System; using Microsoft.SharePoint.Client; using Microsoft.SharePoint.Client.WebParts;  namespace SampleCode {     class UpdateWebPartTitle     {         static void Main()         {             ClientContext oClientContext = new ClientContext("http://MyServer/sites/MySiteCollection");             File oFile = oClientContext.Web.GetFileByServerRelativeUrl("Default.aspx");             LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);              oClientContext.Load(limitedWebPartManager.WebParts,                 wps => wps.Include(                 wp => wp.WebPart.Title));              oClientContext.ExecuteQuery();              if (limitedWebPartManager.WebParts.Count == 0)             {                 throw new Exception("No Web Parts on this page.");             }              WebPartDefinition oWebPartDefinition = limitedWebPartManager.WebParts[1];             WebPart oWebPart = oWebPartDefinition.WebPart;             oWebPart.Title = "My New Web Part Title";              oWebPartDefinition.SaveWebPartChanges();              oClientContext.ExecuteQuery();          }     } }

Featured Posts

Kali Linux Remote Desktop: Access GNOME from Windows Using Native RDP

  Kali Linux + GNOME 50 + GNOME Remote Desktop + Windows Remote Desktop (MSTSC) Getting a full GNOME desktop remotely on Kali Linux can be ...