/// <summary> /// This Method delete right zone from project landing page /// </summary> /// <param name="clientContext">Client context</param> /// <param name="projectList">Project properties</param> private static void DeleteProjectWebparts(ClientContext clientContext, List projectList) { string folderGuid = projectList.EntityTypeName; int pageId = RetrieveItemId(clientContext, Constants.SITEPAGES, folderGuid); if (-1 != pageId) { List list = clientContext.Web.Lists.GetByTitle(Constants.SITE); ListItem listItem = list.GetItemById(pageId); clientContext.Load(list); clientContext.Load(listItem, item => item.File); File file = listItem.File; clientContext.Load(file); LimitedWebPartManager limitedWebPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared); clientContext.Load(limitedWebPartManager.WebParts, wps => wps.Include(wp => wp.WebPart.Properties, wp => wp.WebPart.Title)); WebPartDefinitionCollection webPartDefinition = limitedWebPartManager.WebParts; clientContext.Load(webPartDefinition, item => item.Include(li => li.ZoneId)); clientContext.ExecuteQuery(); var rightZoneWebParts = webPartDefinition.Where(webpart => webpart.ZoneId.Equals(Constants.RIGHTZONE)); foreach (WebPartDefinition zone in rightZoneWebParts) { zone.DeleteWebPart(); } clientContext.ExecuteQuery(); } }
public static void CloseDefaultWebPart(ClientContext clientContext, WebPartDefinitionCollection webPartDefinitionCollection) { foreach (var WebPart in webPartDefinitionCollection) { clientContext.Load(WebPart, w => w.WebPart.Title); clientContext.ExecuteQuery(); if (WebPart.WebPart.Title == "") { WebPart.CloseWebPart(); WebPart.SaveWebPartChanges(); clientContext.ExecuteQuery(); } } }
/// <summary> /// Function to update project landing page as per new project name /// </summary> /// <param name="siteContext">Client context</param> /// <param name="projectToUpdate">Details of project to be updated</param> /// <param name="guid">Project GUID</param> /// <param name="calendarStatus">If Calendar is created</param> /// <param name="taskStatus">If task is created</param> /// <param name="rssFeedStatus">If RSS feed is created</param> public static void UpdateProjectLandingPage(ClientContext siteContext, Project projectToUpdate, string guid, bool calendarStatus, bool taskStatus, bool rssFeedStatus, IConfigurationRoot configuration) { int listID = RetrieveItemId(siteContext, Constants.SITE, guid); try { if (-1 != listID) { List list = siteContext.Web.Lists.GetByTitle(Constants.SITE); ListItem listItem = list.GetItemById(listID); siteContext.Load(listItem); Microsoft.SharePoint.Client.File file = listItem.File; listItem["Title"] = projectToUpdate.NewProjectName; listItem.Update(); /// Update stamped project and project GUID on project landing page LimitedWebPartManager wpm = file.GetLimitedWebPartManager(PersonalizationScope.Shared); siteContext.Load(wpm.WebParts, wps => wps.Include(wp => wp.WebPart.Properties, wp => wp.WebPart.Title)); siteContext.ExecuteQuery(); WebPartDefinitionCollection oWebPartDefinition = wpm.WebParts; siteContext.Load(oWebPartDefinition, item => item.Include(li => li.ZoneId, li => li.WebPart, li => li.WebPart.Properties)); siteContext.ExecuteQuery(); foreach (WebPartDefinition zone in oWebPartDefinition) { if (zone.WebPart.Title.ToLower() == "rss viewer") { rssFeedStatus = true; break; } } foreach (WebPartDefinition zone in oWebPartDefinition) { if (("HeaderZone").Equals(zone.ZoneId)) { zone.DeleteWebPart(); break; } } string catalogAssetsPathStyles = configuration.GetSection("General")["CatalogAssetsStyles"]; string sharePointDLLVersion = configuration.GetSection("General")["SharePointDLLVersion"]; string matterLandingcss = configuration.GetSection("General")["ProjectLandingCss"]; string projectLandingAssets = configuration.GetSection("General")["ProjectLandingAssets"]; string projectCenterAssets = configuration.GetSection("General")["ProjectCenterAssets"]; string projectLandingSectionContent = "<div id=\"{0}\"></div>"; string styleTag = "<link href=\"{0}\" type=\"text/css\" rel=\"stylesheet\" />"; string cssLink = string.Format(CultureInfo.InvariantCulture, catalogAssetsPathStyles + matterLandingcss, projectCenterAssets + Constants.FRONTSLASH + projectLandingAssets); string commonCssLink = string.Format(CultureInfo.InvariantCulture, catalogAssetsPathStyles + Constants.COMMONCSS, projectCenterAssets + Constants.FRONTSLASH + Constants.COMMONASSETS); string headerWebPartSection = string.Format(CultureInfo.InvariantCulture, projectLandingSectionContent, Constants.MATTERCENTERHEADER); string projectLandingStampProperties = "var documentLibraryName = \"{0}\", isNewProjectLandingPage = true, documentLibraryGUID=\"{1}\";"; string scriptTagWithContents = "<script type=\"text/javascript\">{0}</script>"; headerWebPartSection = string.Concat(string.Format(CultureInfo.InvariantCulture, styleTag, cssLink), headerWebPartSection); headerWebPartSection = string.Concat(string.Format(CultureInfo.InvariantCulture, styleTag, commonCssLink), headerWebPartSection); headerWebPartSection = string.Concat(string.Format(CultureInfo.InvariantCulture, scriptTagWithContents, string.Format(CultureInfo.InvariantCulture, projectLandingStampProperties, projectToUpdate.NewProjectName, guid)), headerWebPartSection); string headerWebPart = string.Format(CultureInfo.InvariantCulture, Constants.ContentEditorWebPart, headerWebPartSection, sharePointDLLVersion); WebPartDefinition webPartDefinition = null; if (!string.IsNullOrWhiteSpace(headerWebPart)) { LimitedWebPartManager limitedWebPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared); webPartDefinition = limitedWebPartManager.ImportWebPart(headerWebPart); limitedWebPartManager.AddWebPart(webPartDefinition.WebPart, "HeaderZone", 1); siteContext.ExecuteQuery(); } // Remove existing web-parts from page and recreate List projectList = siteContext.Web.Lists.GetByTitle(projectToUpdate.NewProjectName); siteContext.Load(projectList, lst => lst.Title, lst => lst.EntityTypeName); siteContext.ExecuteQuery(); DeleteProjectWebparts(siteContext, projectList); CreateOneNote(siteContext, projectList, projectToUpdate.NewProjectName, configuration); if (taskStatus) { CreateWebPart(siteContext, projectList, projectToUpdate.NewProjectName, Constants.TASK, Constants.TASKPANE, 2, configuration); } if (calendarStatus) { CreateWebPart(siteContext, projectList, projectToUpdate.NewProjectName, Constants.CALENDAR, Constants.CALENDARPANE, 3, configuration); } if (rssFeedStatus) { CreateWebPart(siteContext, projectList, projectToUpdate.NewProjectName, Constants.RSSTITLE, Constants.RSSPANE, 4, configuration); CreateWebPart(siteContext, projectList, projectToUpdate.NewProjectName, Constants.RSSFEED, Constants.RSSPANE, 5, configuration); } } else { //frmMain.status.UpdateStatus("Project landing page not found with GUID: " + projectToUpdate.ProjectGUID, MatterCenter.Common.Message.MessageType.Error); } } catch (Exception exception) { throw; } }
private static bool SetViewForWebpart(string fileUrl, ClientContext context, List list, string webpartName) { //Setup bool result = true; _log.Debug("Inside SetViewForWebpart"); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); try { File file = context.Web.GetFileByServerRelativeUrl(fileUrl); LimitedWebPartManager webPartManager = file.GetLimitedWebPartManager(PersonalizationScope.Shared); context.Load(webPartManager, w => w.WebParts, w => w.WebParts.Include(wp => wp.WebPart), w => w.WebParts.Include(wp => wp.WebPart.Properties)); context.ExecuteQueryRetry(); WebPartDefinitionCollection webPartDefinitionCollection = webPartManager.WebParts; foreach (WebPartDefinition webPartDefinition in webPartDefinitionCollection) { WebPart webpart = webPartDefinition.WebPart; Dictionary <string, object> propertyValue = webpart.Properties.FieldValues; if (propertyValue["Title"].ToString().Equals(webpartName)) { View listView = list.Views.GetById(webPartDefinition.Id); var viewFields = listView.ViewFields; context.Load(listView, l => l.ListViewXml); context.Load(viewFields); context.ExecuteQueryRetry(); if (true) { int styleId = 0; //parse xml XmlDocument doc = new XmlDocument(); doc.LoadXml(listView.ListViewXml); XmlElement element = (XmlElement)doc.SelectSingleNode("//View//ViewStyle"); if (element == null) { element = doc.CreateElement("ViewStyle"); element.SetAttribute("ID", styleId.ToString()); doc.DocumentElement.AppendChild(element); } else { element.SetAttribute("ID", styleId.ToString()); } listView.ListViewXml = doc.FirstChild.InnerXml; //Performance listView.Update(); context.ExecuteQueryRetry(); } webpart.Properties["InplaceSearchEnabled"] = false; webpart.Properties["DisableViewSelectorMenu"] = true; webPartDefinition.SaveWebPartChanges(); context.ExecuteQueryRetry(); if (true) { listView.ViewFields.RemoveAll(); listView.ViewFields.Add("ID"); listView.ViewFields.Add("FileLeafRef"); } listView.RowLimit = 30; listView.TabularView = false; listView.Update(); context.ExecuteQueryRetry(); } } } catch (Exception e) { result = false; _log.Error(e, "Error inside SetViewForWebpart"); } stopwatch.Stop(); _log.Debug($"It took {stopwatch.Elapsed} to set the view"); return(true); }