예제 #1
0
        private Dictionary <string, string> ReplaceWebPartWebSpecificIDsWithTokens(ClientContext ctx, Web web,
                                                                                   string fileUrl, LimitedWebPartManager webPartManager, Dictionary <Guid, List> listIds)
        {
            var webPartsXml = new Dictionary <string, string>();

            foreach (var wpmWebPart in webPartManager.WebParts)
            {
                var part = RequestContextCredentials == null
                    ? WebPartUtility.GetWebPart(ctx, web, fileUrl, wpmWebPart.Id)
                           .Replace("\r", "")
                           .Replace("\n", "")
                           .Trim()
                           .Replace("  ", " ")
                           .Replace("  ", " ")
                    : WebPartUtility.GetWebPart(web, RequestContextCredentials, fileUrl, wpmWebPart.Id)
                           .Replace("\r", "")
                           .Replace("\n", "")
                           .Trim()
                           .Replace("  ", " ")
                           .Replace("  ", " ");

                part = TokenizeText(web, listIds, part);
                webPartsXml[wpmWebPart.Id.ToString().ToLower().Replace("{", "").Replace("}", "")] = part;
            }
            return(webPartsXml);
        }
예제 #2
0
        private void SetPageListViews(ClientContext ctx, Web web, Dictionary <string, string> newIdMappings)
        {
            Dictionary <string, string> viewCollection;

            if (WikiPageWebPartListViews == null || WikiPageWebPartListViews.Count == 0)
            {
                viewCollection = WebPartPageWebPartListViews;
            }
            else
            {
                viewCollection = WikiPageWebPartListViews;
            }

            if (viewCollection == null || viewCollection.Count == 0)
            {
                return;
            }

            var shouldExecuteQuery = false;

            var soapFailed = false;

            foreach (var webPartId in WebParts.Keys)
            {
                var listTitle = WebParts[webPartId].GetInnerText("{@ListId:", "}", true);
                if (viewCollection.ContainsKey(webPartId))
                {
                    if (!soapFailed)
                    {
                        try
                        {
                            //Get the real view id from the web part from the page
                            var partXml = WebPartUtility.GetWebPart(ctx, web, File.ServerRelativeUrl,
                                                                    Guid.Parse(newIdMappings[webPartId]));
                            var viewId = partXml.GetInnerText("View Name=\"", "\"", true);
                            var list   = web.Lists.GetByTitle(listTitle);
                            var view   = list.Views.GetById(Guid.Parse(viewId));
                            view.ListViewXml = viewCollection[webPartId];
                            view.Update();
                            shouldExecuteQuery = true;
                        }
                        catch
                        {
                            soapFailed = true;
                        }
                    }

                    if (soapFailed)
                    {
                        var list = ctx.Web.Lists.GetByTitle(listTitle);
                        ctx.Load(list.Views,
                                 v => v.Include(view => view.Id, view => view.Hidden).Where(view => view.Hidden));

                        ctx.ExecuteQueryRetry();

                        var viewCount = list.Views.Count;
                        var lastView  = list.Views[viewCount - 1];
                        lastView.ListViewXml = viewCollection[webPartId];
                        lastView.Update();
                        shouldExecuteQuery = true;
                    }
                }
            }
            if (shouldExecuteQuery)
            {
                ctx.ExecuteQueryRetry();
            }
        }