Example #1
0
 //Original for backward compatability of older manifests pre-builder style
 protected void UpdateViews()
 {
     if (ViewSchemas != null && ViewSchemas.Count > 0)
     {
         foreach (var key in ViewSchemas.Keys)
         {
             if (ViewMappings.ContainsKey(key))
             {
                 Views[ViewMappings[key]].ListViewXml = ViewSchemas[key];
                 Views[ViewMappings[key]].Update();
             }
         }
     }
     if (ViewMappings2 != null && ViewMappings2.Count > 0)
     {
         foreach (var viewMapping in ViewMappings2)
         {
             if (WebPartPageWebPartListViews.ContainsKey(viewMapping.Key))
             {
                 Views[viewMapping.Value].ListViewXml = WebPartPageWebPartListViews[viewMapping.Key];
                 Views[viewMapping.Value].Update();
             }
         }
     }
 }
Example #2
0
        public void AddWebPartPageWebParts(ClientContext ctx, Web web)
        {
            var rootWeb = ctx.Site.RootWeb;

            rootWeb.EnsureProperties(w => w.Url, w => w.ServerRelativeUrl);
            var limitedWebPartManager = File.GetLimitedWebPartManager(PersonalizationScope.Shared);
            var newIdMappings         = new Dictionary <string, string>();

            //Add from last to first in the zone to
            //avoid dealing with Sequence
            var orderedWebParts = WebPartPageZoneMappings.OrderByDescending(zm => zm.Position);

            foreach (var zoneMapping in orderedWebParts)
            {
                var wpXml = WebParts[zoneMapping.WebPartId]
                            .Replace("{@WebUrl}", web.Url)
                            .Replace("{@WebServerRelativeUrl}", web.ServerRelativeUrl != "/" ? web.ServerRelativeUrl : "")
                            .Replace("{@SiteUrl}", rootWeb.Url)
                            .Replace("{@SiteServerRelativeUrl}", rootWeb.ServerRelativeUrl != "/" ? rootWeb.ServerRelativeUrl : "");

                //This is a crude way to go about cooercing the version numbers
                //for files created on SPO/SP2016 for SP2013 on prem
                //probably valid 99.999% of the time, but if it isn't for you, sorry!
                if (ctx.ServerLibraryVersion.Major == 15)
                {
                    wpXml = wpXml.Replace("16.0.0.0", "15.0.0.0");
                }

                var hasListIdToken  = wpXml.Contains(@"{@ListId:");
                var hasListUrlToken = wpXml.Contains(@"{@ListUrl:");
                var listTitle       = string.Empty;

                if (hasListIdToken || hasListUrlToken)
                {
                    listTitle = wpXml.GetInnerText("{@ListId:", "}");
                    wpXml     = ReplaceListTokens(wpXml, ctx, web, false);
                }

                var def = limitedWebPartManager.ImportWebPart(wpXml);
                def = limitedWebPartManager.AddWebPart(def.WebPart, zoneMapping.ZoneId, 0);
                ctx.Load(def);
                ctx.Load(def.WebPart);
                ctx.ExecuteQueryRetry();

                newIdMappings[zoneMapping.WebPartId] = def.Id.ToString().Replace("{", "").Replace("}", "").ToLower();

                if (WebPartPageWebPartListViews.ContainsKey(zoneMapping.WebPartId))
                {
                    //Fallback code for lack of WebPartPages web service to app identities
                    //Need config to do one or other instead of failing over
                    GetNewView(ctx, zoneMapping.WebPartId, listTitle);
                }
            }
            SetPageListViews(ctx, web, newIdMappings);
        }