I have SharePoint Site on SharePoint online with Pages library having 5 pages with content. Need to modify the URL on the pages programmatically using Client Object Model
1 Answer
Here is a console application that does what you want. Below i am getting the file by server relative URL, you could either get them directly by iterating the collection of items from the list.
string pass = "<your-password>";
SecureString ss = new SecureString();
foreach (char c in pass)
{
ss.AppendChar(c);
}
using (ClientContext ClientWebContext = new ClientContext("<site-collection-url"))
{
ClientWebContext.Credentials = new SharePointOnlineCredentials("<your-username>", ss);
List li = ClientWebContext.Web.Lists.GetByTitle("Pages");
Microsoft.SharePoint.Client.File f = ClientWebContext.Web.GetFileByServerRelativeUrl("<relative-path-of-your-file>");
ListItem item = f.ListItemAllFields;
ClientWebContext.Load(item, i => i["FileLeafRef"]);
item.File.CheckOut();
ClientWebContext.ExecuteQuery();
item["FileLeafRef"] = "Newname.aspx";//some new file name
item.Update();
ClientWebContext.Load(item);
ClientWebContext.ExecuteQuery();
item.File.CheckIn("Check-in",CheckinType.MajorCheckIn);
ClientWebContext.ExecuteQuery();
}