protected WebPart FindExistingWebPart( IEnumerable <Microsoft.SharePoint.Client.WebParts.WebPartDefinition> webPartDefenitions, WebPartDefinitionBase webPartModel) { Microsoft.SharePoint.Client.WebParts.WebPartDefinition wpDefinition = null; return(FindExistingWebPart(webPartDefenitions, webPartModel, out wpDefinition)); }
//protected void WithExistingWebPart(ListItem listItem, WebPartDefinition webPartModel, // Action<WebPart, Microsoft.SharePoint.Client.WebParts.WebPartDefinition> action) //{ // WithExistingWebPart(listItem.File, webPartModel, action); //} protected void WithExistingWebPart(File pageFile, WebPartDefinition webPartModel, Action <WebPart, Microsoft.SharePoint.Client.WebParts.WebPartDefinition> action) { var context = pageFile.Context; var webPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared); // web part on the page var webpartOnPage = webPartManager.WebParts.Include(wp => wp.Id, wp => wp.WebPart); var webPartDefenitions = context.LoadQuery(webpartOnPage); context.ExecuteQueryWithTrace(); Microsoft.SharePoint.Client.WebParts.WebPartDefinition def = null; var existingWebPart = FindExistingWebPart(webPartDefenitions, webPartModel, out def); action(existingWebPart, def); }
protected void WithWithExistingWebPart(ListItem listItem, WebPartDefinition webPartModel, Action <WebPart, Microsoft.SharePoint.Client.WebParts.WebPartDefinition> action) { var context = listItem.Context; var filePath = listItem["FileRef"].ToString(); var web = listItem.ParentList.ParentWeb; var pageFile = web.GetFileByServerRelativeUrl(filePath); var webPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared); // web part on the page var webpartOnPage = webPartManager.WebParts.Include(wp => wp.Id, wp => wp.WebPart); var webPartDefenitions = context.LoadQuery(webpartOnPage); context.ExecuteQueryWithTrace(); Microsoft.SharePoint.Client.WebParts.WebPartDefinition def = null; var existingWebPart = FindExistingWebPart(webPartDefenitions, webPartModel, out def); action(existingWebPart, def); }
protected WebPart FindExistingWebPart(IEnumerable <Microsoft.SharePoint.Client.WebParts.WebPartDefinition> webPartDefenitions, WebPartDefinitionBase webPartModel, out Microsoft.SharePoint.Client.WebParts.WebPartDefinition wpDefinition) { TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Resolving web part by Title: [{0}]", webPartModel.Title); wpDefinition = null; // gosh, you got to be kidding // internally, SharePoint returns StorageKey as ID. hence.. no ability to trace unique web part on the page // the only thing is comparing Titles an utilize them as a primary key foreach (var webPartDefinition in webPartDefenitions) { if (String.Compare(webPartDefinition.WebPart.Title, webPartModel.Title, System.StringComparison.OrdinalIgnoreCase) == 0) { wpDefinition = webPartDefinition; return(webPartDefinition.WebPart); } } return(null); }
public override void DeployModel(object modelHost, DefinitionBase model) { Guid?OldWebParKey = null; var listItemModelHost = modelHost.WithAssertAndCast <ListItemModelHost>("modelHost", value => value.RequireNotNull()); var webPartModel = model.WithAssertAndCast <WebPartDefinitionBase>("model", value => value.RequireNotNull()); try { OnBeforeDeploy(listItemModelHost, webPartModel); CurrentClientContext = listItemModelHost.HostClientContext; var context = listItemModelHost.HostClientContext; var currentPageFile = GetCurrentPageFile(listItemModelHost); if (listItemModelHost.HostFolder != null) { // TODO, re-implement for SP2010 CSOM // the following stuff is needed only for the web part deployment to the non-web part pages // like, view/upload/details pages in the lost/libs // hope no one would use that case on 2010 - folks, migrate to 2013 at least! :) #if !NET35 if (!listItemModelHost.HostFolder.IsPropertyAvailable("Properties") || listItemModelHost.HostFolder.Properties.FieldValues.Count == 0) { listItemModelHost.HostFolder.Context.Load(listItemModelHost.HostFolder, f => f.Properties); //folder.Context.Load(folder, f => f.Properties); listItemModelHost.HostFolder.Context.ExecuteQueryWithTrace(); } #endif } #if !NET35 var doesFileHasListItem = //Forms folders !(listItemModelHost.HostFolder != null && (listItemModelHost.HostFolder.Properties.FieldValues.ContainsKey("vti_winfileattribs") && listItemModelHost.HostFolder.Properties.FieldValues["vti_winfileattribs"].ToString() == "00000012")); #endif #if NET35 // TODO, re-implement for SP2010 CSOM // the following stuff is needed only for the web part deployment to the non-web part pages var doesFileHasListItem = true; #endif ModuleFileModelHandler.WithSafeFileOperation(listItemModelHost.HostList, currentPageFile, pageFile => { Guid?webPartStoreKey = null; InternalOnBeforeWebPartProvision(new WebPartProcessingContext { ListItemModelHost = listItemModelHost, WebPartDefinition = webPartModel, WebPartStoreKey = webPartStoreKey }); //var fileContext = pageFile.Context; ListItem fileListItem = null; if (webPartModel.AddToPageContent) { // pre load here to be used later var fileContext = pageFile.Context; fileListItem = pageFile.ListItemAllFields; fileContext.Load(fileListItem); fileContext.ExecuteQueryWithTrace(); } var webPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared); // web part on the page var webpartOnPage = webPartManager.WebParts.Include(wp => wp.Id, wp => wp.WebPart); var webPartDefenitions = context.LoadQuery(webpartOnPage); context.ExecuteQueryWithTrace(); Microsoft.SharePoint.Client.WebParts.WebPartDefinition wpDefinition; WebPart existingWebPart = null; // TODO var tmpWp = FindExistingWebPart(webPartDefenitions, webPartModel, out wpDefinition); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = existingWebPart, ObjectType = typeof(WebPart), ObjectDefinition = webPartModel, ModelHost = modelHost }); if (wpDefinition != null) { OldWebParKey = wpDefinition.Id; TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Deleting current web part."); wpDefinition.DeleteWebPart(); wpDefinition.Context.ExecuteQueryWithTrace(); } else { existingWebPart = tmpWp; } Microsoft.SharePoint.Client.WebParts.WebPartDefinition webPartAddedDefinition = null; if (existingWebPart == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new web part"); var webPartXML = GetWebpartXmlDefinition(listItemModelHost, webPartModel); webPartXML = ProcessCommonWebpartProperties(webPartXML, webPartModel); //// handle wiki page //if (webPartModel.AddToPageContent) //{ // HandleWikiPageProvision(fileListItem, webPartModel); //} var webPartDefinition = webPartManager.ImportWebPart(webPartXML); webPartAddedDefinition = webPartManager.AddWebPart(webPartDefinition.WebPart, webPartModel.ZoneId, webPartModel.ZoneIndex); context.Load(webPartAddedDefinition); context.ExecuteQueryWithTrace(); if (webPartAddedDefinition != null && webPartAddedDefinition.ServerObjectIsNull == false) { existingWebPart = webPartAddedDefinition.WebPart; webPartStoreKey = webPartAddedDefinition.Id; } // handle wiki page if (webPartModel.AddToPageContent) { HandleWikiPageProvision(fileListItem, webPartModel, webPartStoreKey, OldWebParKey); } existingWebPart = webPartDefinition.WebPart; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = existingWebPart, ObjectType = typeof(WebPart), ObjectDefinition = webPartModel, ModelHost = modelHost }); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing web part"); if (webPartModel.AddToPageContent) { //HandleWikiPageProvision(fileListItem, webPartModel); } InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = existingWebPart, ObjectType = typeof(WebPart), ObjectDefinition = webPartModel, ModelHost = modelHost }); } context.ExecuteQueryWithTrace(); if (webPartAddedDefinition != null && webPartAddedDefinition.ServerObjectIsNull == false) { existingWebPart = webPartAddedDefinition.WebPart; webPartStoreKey = webPartAddedDefinition.Id; } InternalOnAfterWebPartProvision(new WebPartProcessingContext { ListItemModelHost = listItemModelHost, WebPartDefinition = webPartModel, WebPartStoreKey = webPartStoreKey }); return(pageFile); }, doesFileHasListItem); } finally { OnAfterDeploy(listItemModelHost, webPartModel); } }
public override void DeployModel(object modelHost, DefinitionBase model) { var listItemModelHost = modelHost.WithAssertAndCast <ListItemModelHost>("modelHost", value => value.RequireNotNull()); var webPartModel = model.WithAssertAndCast <WebPartDefinitionBase>("model", value => value.RequireNotNull()); var listItem = listItemModelHost.HostListItem; var context = listItem.Context; var currentPageFile = GetCurrentPageFile(listItemModelHost); ModuleFileModelHandler.WithSafeFileOperation(listItem.ParentList, currentPageFile, pageFile => { Guid?webPartStoreKey = null; InternalOnBeforeWebPartProvision(new WebPartProcessingContext { ListItemModelHost = listItemModelHost, WebPartDefinition = webPartModel, WebPartStoreKey = webPartStoreKey }); //var fileContext = pageFile.Context; var fileListItem = pageFile.ListItemAllFields; var fileContext = pageFile.Context; fileContext.Load(fileListItem); fileContext.ExecuteQueryWithTrace(); var webPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared); // web part on the page var webpartOnPage = webPartManager.WebParts.Include(wp => wp.Id, wp => wp.WebPart); var webPartDefenitions = context.LoadQuery(webpartOnPage); context.ExecuteQueryWithTrace(); Microsoft.SharePoint.Client.WebParts.WebPartDefinition wpDefinition; WebPart existingWebPart = null; // TODO var tmpWp = FindExistingWebPart(webPartDefenitions, webPartModel, out wpDefinition); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioning, Object = existingWebPart, ObjectType = typeof(WebPart), ObjectDefinition = webPartModel, ModelHost = modelHost }); if (wpDefinition != null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Deleting current web part."); wpDefinition.DeleteWebPart(); wpDefinition.Context.ExecuteQueryWithTrace(); } else { existingWebPart = tmpWp; } Microsoft.SharePoint.Client.WebParts.WebPartDefinition webPartAddedDefinition = null; if (existingWebPart == null) { TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject, "Processing new web part"); var webPartXML = GetWebpartXmlDefinition(listItemModelHost, webPartModel); webPartXML = ProcessCommonWebpartProperties(webPartXML, webPartModel); // handle wiki page HandleWikiPageProvision(fileListItem, webPartModel); var webPartDefinition = webPartManager.ImportWebPart(webPartXML); webPartAddedDefinition = webPartManager.AddWebPart(webPartDefinition.WebPart, webPartModel.ZoneId, webPartModel.ZoneIndex); context.Load(webPartAddedDefinition); InvokeOnModelEvent <WebPartDefinition, WebPart>(null, ModelEventType.OnUpdating); existingWebPart = webPartDefinition.WebPart; InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = existingWebPart, ObjectType = typeof(WebPart), ObjectDefinition = webPartModel, ModelHost = modelHost }); InvokeOnModelEvent <WebPartDefinition, WebPart>(null, ModelEventType.OnUpdated); } else { TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject, "Processing existing web part"); HandleWikiPageProvision(fileListItem, webPartModel); InvokeOnModelEvent(this, new ModelEventArgs { CurrentModelNode = null, Model = null, EventType = ModelEventType.OnProvisioned, Object = existingWebPart, ObjectType = typeof(WebPart), ObjectDefinition = webPartModel, ModelHost = modelHost }); } context.ExecuteQueryWithTrace(); if (webPartAddedDefinition != null && webPartAddedDefinition.ServerObjectIsNull == false) { existingWebPart = webPartAddedDefinition.WebPart; webPartStoreKey = webPartAddedDefinition.Id; } InternalOnAfterWebPartProvision(new WebPartProcessingContext { ListItemModelHost = listItemModelHost, WebPartDefinition = webPartModel, WebPartStoreKey = webPartStoreKey }); return(pageFile); }); }