Skip to content

Instantly share code, notes, and snippets.

@Niels-V
Created February 19, 2014 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Niels-V/9102200 to your computer and use it in GitHub Desktop.
Save Niels-V/9102200 to your computer and use it in GitHub Desktop.
Change all webpart titles in a specific SP2010 pages library by CSOM.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.WebParts;
namespace UpdateWebPartTitles
{
class Program
{
static void Main(string[] args)
{
ChangeWebPartTitle source = new ChangeWebPartTitle();
source.Login(args[0], args[1], args[2], args[3]);
source.ChangeWebPartTitles(args[4], args[5], args[6]);
}
}
class ChangeWebPartTitle
{
private ClientContext clientContext { get; set; }
private string OldTitle { get; set; }
private string NewTitle { get; set; }
internal void Login(string webUrl, string username, string domain, string password)
{
clientContext = new ClientContext(webUrl);
ICredentials creds = new NetworkCredential(username, password, domain);
clientContext.Credentials = creds;
}
internal void ChangeWebPartTitles(string oldValue, string newValue, string pagesListTitle)
{
OldTitle = oldValue;
NewTitle = newValue;
Web web = clientContext.Web;
clientContext.Load(web, w => w.Webs, w => w.Title, w => w.Lists);
clientContext.ExecuteQuery();
var pagesList = web.Lists.GetByTitle(pagesListTitle);
clientContext.Load(pagesList, p=> p.Title);
clientContext.ExecuteQuery();
ProcessList(web, pagesList);
}
private void ProcessList(Web web, List list)
{
ListItemCollectionPosition itemPosition = null;
while (true)
{
CamlQuery camlQuery = CamlQuery.CreateAllItemsQuery();
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
itemPosition = listItems.ListItemCollectionPosition;
foreach (ListItem listItem in listItems)
{
File file = listItem.File;
clientContext.Load(file, f => f.CheckOutType, f => f.Title);
UpdateWebPartTitles(listItem.File);
Approve(listItem);
}
if (itemPosition == null)
break;
}
}
private void Approve(ListItem listItem)
{
int status = Convert.ToInt32(listItem["_ModerationStatus"]);
if (status != 0)
{
listItem["_ModerationStatus"] = 0;
listItem.Update();
clientContext.ExecuteQuery();
}
}
private void UpdateWebPartTitles(File file)
{
LimitedWebPartManager limitedWebPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
clientContext.Load(limitedWebPartManager.WebParts,
wps => wps.Include(
wp => wp.WebPart.Title));
clientContext.ExecuteQuery();
if (limitedWebPartManager.WebParts.Count == 0)
{
return;
}
bool pageChanged = false;
foreach (var wpd in limitedWebPartManager.WebParts)
{
bool webpartChanged = ChangeWebPartTitle(file, wpd);
pageChanged = pageChanged || webpartChanged;
}
if (pageChanged)
{
file.CheckIn("Changed WPTitle", CheckinType.MajorCheckIn);
file.Publish("Changed WPTitle");
clientContext.ExecuteQuery();
}
}
/// <summary>
/// Changes the web part title.
/// </summary>
/// <param name="webpartPage">The WebPart Page.</param>
/// <param name="wpd">The WebPartDefinition.</param>
/// <returns><c>true</c> when the webpart has changed, <c>false</c> otherwise.</returns>
private bool ChangeWebPartTitle(File webpartPage, WebPartDefinition wpd)
{
WebPart webpart = wpd.WebPart;
if (webpart.Title.ToLowerInvariant() == OldTitle.ToLowerInvariant())
{
if (webpartPage.CheckOutType == CheckOutType.None)
{
webpartPage.CheckOut();
clientContext.ExecuteQuery();
}
webpart.Title = NewTitle;
wpd.SaveWebPartChanges();
return true;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment