Exemplo n.º 1
0
        public void ShouldGetItemVersionContent()
        {
            var svc = new ListsSoapClient();

            svc.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(_appSettings.Username, _appSettings.Password, "SSW2000");
            var xx = svc.GetVersionCollection("Pages", "4", "PublishingPageContent");

            xx.Should().NotBeNull();
        }
        /// <summary>
        ///  need to hit 2 different services in order to get full history: CSOM and lists.asmx
        /// </summary>
        /// <param name="contentItem"></param>
        /// <param name="ctx"></param>
        /// <param name="item"></param>
        private void LoadContentHistory(IHasContentHistory contentItem, ClientContext ctx, ListItem item)
        {
            _log.LogInformation("load history for item {item} ", item.Id);

            var introTextFieldName = (item.ContentType.Name == "RuleSummaryPage") ? "RuleSummaryIntro" : "RuleContentTop";

            var file = ctx.Web.GetFileByServerRelativeUrl($"/Pages/{item["FileLeafRef"]}");

            ctx.Load(file);

            var versions = file.Versions;

            ctx.Load(versions);
            var oldVersions = ctx.LoadQuery(versions.Where(v => v != null));

            ctx.ExecuteQuery();

            var svc = new ListsSoapClient();

            svc.ClientCredentials.Windows.ClientCredential = new System.Net.NetworkCredential(_appSettings.Username, _appSettings.Password, "SSW2000");

            XNamespace xmlns = "http://schemas.microsoft.com/sharepoint/soap/";

            var fields = new List <string> {
                "Title", introTextFieldName, "PublishingPageContent"
            };
            // we need to fetch history for each field individually - and we might not have data for every record reported by the "Version" field
            // build this data into a 2-level dictionary fieldName->modifiedDate->Value
            // use of modified date key ensures we map incomplete datasets to correct versions
            var fieldHistoryData = new Dictionary <string, Dictionary <string, string> >();

            foreach (var field in fields)
            {
                fieldHistoryData[field] = new Dictionary <string, string>();
                var fieldVersionsXml = svc.GetVersionCollection("Pages", item.Id.ToString(), field);
                var elements         = fieldVersionsXml.Descendants(xmlns + "Version");
                foreach (var element in elements) // note: I started with a ToDictionary() here but we need to handle duplicate 'modified' value in source data
                {
                    var modified = element.Attribute("Modified")?.Value;
                    if (!string.IsNullOrWhiteSpace(modified) && !fieldHistoryData[field].ContainsKey(modified))
                    {
                        fieldHistoryData[field][modified] = element.Attribute(field)?.Value;
                    }
                }
            }

            // now fetch all data for the "Version" field to make ContentVersion objects - adding fields from fieldHistoryData where we can
            var versionsXml     = svc.GetVersionCollection("Pages", item.Id.ToString(), "Version");
            var contentVersions = versionsXml.Descendants(xmlns + "Version")
                                  .Select(v => new ContentVersion()
            {
                VersionLabel = v.Attribute("Version")?.Value,
                Comment      = oldVersions.FirstOrDefault(x => x.VersionLabel == v.Attribute("Version")?.Value)
                               ?.CheckInComment,
                ModifiedUtc        = DateTime.Parse(v.Attribute("Modified")?.Value),
                ModifiedBy         = v.Attribute("Editor")?.Value?.Split(new char[] { ',' })[2],
                ModifiedByFullName = v.Attribute("Editor")?.Value?.Split(new char[] { ',' })[4],
                Title     = fieldHistoryData.ValueOrNull("Title")?.ValueOrNull(v.Attribute("Modified")?.Value),
                IntroText = fieldHistoryData.ValueOrNull(introTextFieldName)?.ValueOrNull(v.Attribute("Modified")?.Value),
                Content   = fieldHistoryData.ValueOrNull("PublishingPageContent")?.ValueOrNull(v.Attribute("Modified")?.Value),
            }).ToList();

            contentItem.Versions = contentVersions;
        }