Ejemplo n.º 1
0
        public void AddWebParts(ClientContext context, File uploadFile, List<ShWebPartReference> webPartReferences, bool replaceWebParts)
        {
            // we should be allowed to delete webparts (by using replaceWebparts and define no new ones
            if (webPartReferences.Count <= 0 && !replaceWebParts) return;

            var limitedWebPartManager = uploadFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

            context.Load(limitedWebPartManager, manager => manager.WebParts);
            context.ExecuteQuery();

            if (limitedWebPartManager.WebParts.Count == 0 || replaceWebParts)
            {
                for (int i = limitedWebPartManager.WebParts.Count - 1; i >= 0; i--)
                {
                    limitedWebPartManager.WebParts[i].DeleteWebPart();
                }
                context.ExecuteQuery();

                foreach (ShWebPartReference webPart in webPartReferences)
                {
                    //Convention: All webparts are located in the content/webparts folder
                    var webPartPath = Path.Combine(_contentDirectoryPath, "webparts", webPart.FileName);
                    var webPartFileContent = System.IO.File.ReadAllText(webPartPath);
                    if (!System.IO.File.Exists(webPartPath))
                    {
                        Log.ErrorFormat("Webpart at path {0} not found", webPartPath);
                        continue;
                    }

                    //Token replacement in the webpart XML
                    webPartFileContent = GetPropertyValueWithTokensReplaced(webPartFileContent, context);
                    var webPartDefinition = limitedWebPartManager.ImportWebPart(webPartFileContent);
                    if (webPart.PropertiesOverrides.Count > 0)
                    {
                        foreach (KeyValuePair<string, string> propertyOverride in webPart.PropertiesOverrides)
                        {
                            //Token replacement in the PropertiesOverrides JSON array
                            var propOverrideValue = GetPropertyValueWithTokensReplaced(propertyOverride.Value, context);
                            webPartDefinition.WebPart.Properties[propertyOverride.Key] = propOverrideValue;
                        }
                    }
                    limitedWebPartManager.AddWebPart(webPartDefinition.WebPart, webPart.ZoneID, webPart.Order);
                }

                context.Load(limitedWebPartManager);
                context.ExecuteQuery();
            }
        }
Ejemplo n.º 2
0
        public void AddWebParts(ClientContext context, File uploadFile, List<ShWebPartReference> webPartReferences, bool replaceWebParts)
        {
            // we should be allowed to delete webparts (by using replaceWebparts and define no new ones
            if (webPartReferences.Count <= 0 && !replaceWebParts) return;

            var limitedWebPartManager = uploadFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

            context.Load(limitedWebPartManager, manager => manager.WebParts);
            context.ExecuteQuery();

            if (limitedWebPartManager.WebParts.Count == 0 || replaceWebParts)
            {
                for (int i = limitedWebPartManager.WebParts.Count - 1; i >= 0; i--)
                {
                    limitedWebPartManager.WebParts[i].DeleteWebPart();
                    context.ExecuteQuery();
                }

                foreach (ShWebPartReference webPart in webPartReferences)
                {
                    //Convention: All webparts are located in the content/webparts folder
                    var webPartPath = Path.Combine(_contentDirectoryPath, "webparts", webPart.FileName);
                    var webPartFileContent = System.IO.File.ReadAllText(webPartPath);
                    if (!System.IO.File.Exists(webPartPath))
                    {
                        Log.ErrorFormat("Webpart at path {0} not found", webPartPath);
                        continue;
                    }

                    //Token replacement in the webpart XML
                    webPartFileContent = GetPropertyValueWithTokensReplaced(webPartFileContent, context);

                    //Overriding DataProviderJSON properties if specified. Need to use different approach (Update XML directly before import)
                    if (webPart.PropertiesOverrides.Count > 0 || webPart.DataProviderJSONOverrides.Count > 0)
                    {
                        webPartFileContent = ReplaceWebPartPropertyOverrides(context, webPart, webPartFileContent);
                    }

                    var webPartDefinition = limitedWebPartManager.ImportWebPart(webPartFileContent);
                    limitedWebPartManager.AddWebPart(webPartDefinition.WebPart, webPart.ZoneID, webPart.Order);
                    context.Load(limitedWebPartManager);
                    context.ExecuteQuery();
                }
            }
        }
Ejemplo n.º 3
0
        //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);
        }
Ejemplo n.º 4
0
 private WebPartDefinition AddWebPart(Web web, WebPart webPart, File pageFile)
 {
     LimitedWebPartManager limitedWebPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
     WebPartDefinition oWebPartDefinition = limitedWebPartManager.ImportWebPart(webPart.Contents);
     WebPartDefinition wpdNew = limitedWebPartManager.AddWebPart(oWebPartDefinition.WebPart, webPart.Zone, (int)webPart.Order);
     web.Context.Load(wpdNew, x => x.Id);
     web.Context.ExecuteQueryRetry();
     return wpdNew;
 }