Esempio n. 1
0
        private static void AddOrUpdateRepostPage(PnPClientContext context, NewsPost post)
        {
            string pageName = $"{post.UrlSlug}.aspx";

            var repostPage = EnsureRepostPage(context, pageName);

            string postUrl = post.PermaLink ?? String.Format(Program.CONFIG.CustomNewsLinkStringTemplate, post.PostId);

            if (postUrl.Length > 255)
            {
                LogWarning("  Repost URL exceeds the maximum allowed length of 255 characters. URL will be truncated to 255 characters.");
                postUrl = postUrl.Substring(0, 255);
            }
            string imageUrl = post.Images.Box960.Url;

            bool     hasChanges = false;
            ListItem repostItem = repostPage.PageListItem;

            // Audience Targeting
            var audienceGroupNames = MapCategoriesToGroupNames(post.Categories.Select(c => c.Name).Distinct().ToArray());
            var resolvedGroups     = ResolveGroups(context, audienceGroupNames);
            var groupFieldValues   = resolvedGroups.Select(rg => new FieldUserValue()
            {
                LookupId = rg.Id
            }).ToList();

            if (SetFieldValue(ref repostItem, "_ModernAudienceTargetUserField", groupFieldValues))
            {
                hasChanges = true;
            }

            // Set Custom Repost Content Type
            if (SetFieldValue(ref repostItem, ClientSidePage.ContentTypeId, Program.CONFIG.CustomRepostContentTypeId))
            {
                hasChanges = true;
            }

            // Set Core Fields
            if (SetFieldValue(ref repostItem, ClientSidePage.Title, post.Title))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, ClientSidePage.DescriptionField, post.Description))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, ClientSidePage.FirstPublishedDate, DateTime.UtcNow, overwrite: false))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, ClientSidePage.PromotedStateField, (int)PromotedState.Promoted))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, ClientSidePage._OriginalSourceUrl, postUrl))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, ClientSidePage._OriginalSourceSiteId, Guid.Empty))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, ClientSidePage._OriginalSourceWebId, Guid.Empty))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, ClientSidePage._OriginalSourceListId, Guid.Empty))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, ClientSidePage._OriginalSourceItemId, Guid.Empty))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, ClientSidePage.BannerImageUrl, new FieldUrlValue()
            {
                Description = imageUrl, Url = imageUrl
            }))
            {
                hasChanges = true;
            }

            // Set Core Repost Content
            string layoutsWebpartContent = GetRepostLayoutWebpartsContent(postUrl, imageUrl, post);

            if (SetFieldValue(ref repostItem, ClientSidePage.PageLayoutContentField, layoutsWebpartContent))
            {
                hasChanges = true;
            }
            if (hasChanges)
            {
                LogInfo("  Updating news link core fields");
                repostItem.Update();
                context.ExecuteQueryRetry();
            }

            // Set Extra Metadata
            var terms = GetManagedMetadataTerms(context, post.Categories.Select(c => c.Name).Distinct().ToArray());

            if (SetFieldValue(ref repostItem, "SourceCategories", terms))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, "SourcePostSourceType", post.PostSourceType))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, "SourceProvider", post.Provider))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, "SourceModifiedDate", post.ModifiedDate))
            {
                hasChanges = true;
            }
            if (SetFieldValue(ref repostItem, "SourcePublishDate", post.PublishDate))
            {
                hasChanges = true;
            }

            if (hasChanges)
            {
                LogInfo("  Updating news link metadata");
                repostItem.Update();
                context.ExecuteQueryRetry();
                repostPage.Publish();
            }
            else
            {
                LogInfo("  News link is up-to-date; no changes made");
            }
        }
Esempio n. 2
0
        private static string GetRepostLayoutWebpartsContent(string postUrl, string imageUrl, NewsPost post)
        {
            string outerTemplate = @"<div><div data-sp-canvascontrol="""" data-sp-canvasdataversion="""" data-sp-controldata=""{0}""></div></div>";
            var    controlData   = new
            {
                id                     = "c1b5736d-84dd-4fdb-a7be-e7e9037bd3c3",
                instanceId             = "c1b5736d-84dd-4fdb-a7be-e7e9037bd3c3",
                serverProcessedContent = new
                {
                    htmlStrings          = new { },
                    searchablePlainTexts = new { },
                    imageSources         = new { },
                    links = new { },
                },
                dataVersion = "1.0",
                properties  = new
                {
                    description       = post.Description,
                    thumbnailImageUrl = imageUrl,
                    title             = post.Title,
                    url = postUrl
                }
            };
            string controlDataJson    = JsonSerializer.Serialize(controlData);
            string encodedControlData = HttpUtility.HtmlEncode(controlDataJson);

            encodedControlData = encodedControlData.Replace("{", "&#123;");
            encodedControlData = encodedControlData.Replace("}", "&#125;");
            encodedControlData = encodedControlData.Replace(":", "&#58;");

            return(string.Format(outerTemplate, encodedControlData));
        }