void RegisterJStoWebPart(Web web, string url, string jsPath) { Microsoft.SharePoint.Client.File newFormPageFile = web.GetFileByServerRelativeUrl(url); LimitedWebPartManager limitedWebPartManager = newFormPageFile.GetLimitedWebPartManager(PersonalizationScope.Shared); web.Context.Load(limitedWebPartManager.WebParts); web.Context.ExecuteQuery(); if (limitedWebPartManager.WebParts.Count > 0) { WebPartDefinition webPartDef = limitedWebPartManager.WebParts.FirstOrDefault(); webPartDef.WebPart.Properties["JSLink"] = jsPath; webPartDef.SaveWebPartChanges(); web.Context.ExecuteQuery(); } }
/// <summary> /// Sets a web part property /// </summary> /// <param name="web">The web to process</param> /// <param name="key">The key to update</param> /// <param name="value">The value to set</param> /// <param name="id">The id of the webpart</param> /// <param name="serverRelativePageUrl"></param> public static void SetWebPartProperty(this Web web, string key, string value, Guid id, string serverRelativePageUrl) { ClientContext context = web.Context as ClientContext; File file = web.GetFileByServerRelativeUrl(serverRelativePageUrl); context.Load(file, f => f.ListItemAllFields); context.ExecuteQuery(); ListItem listItem = file.ListItemAllFields; LimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared); context.Load(wpm.WebParts); WebPartDefinition def = wpm.WebParts.GetById(id); switch (key.ToLower()) { case "title": { def.WebPart.Title = value; break; } case "titleurl": { def.WebPart.TitleUrl = value; break; } default: { def.WebPart.Properties[key] = value; break; } } def.SaveWebPartChanges(); context.ExecuteQuery(); }
public void CloseWebPart() { _webPartDefinition.CloseWebPart(); _webPartDefinition.SaveWebPartChanges(); _webPartDefinition.Context.ExecuteQuery(); }