private Tuple<List, TokenParser> UpdateList(Web web, List existingList, ListInstance templateList, TokenParser parser, PnPMonitoredScope scope) { web.Context.Load(existingList, l => l.Title, l => l.Description, l => l.OnQuickLaunch, l => l.Hidden, l => l.ContentTypesEnabled, l => l.EnableAttachments, l => l.EnableFolderCreation, l => l.EnableMinorVersions, l => l.DraftVersionVisibility, l => l.Views #if !CLIENTSDKV15 , l => l.MajorWithMinorVersionsLimit #endif ); web.Context.ExecuteQueryRetry(); if (existingList.BaseTemplate == templateList.TemplateType) { var isDirty = false; if (parser.ParseString(templateList.Title) != existingList.Title) { existingList.Title = parser.ParseString(templateList.Title); isDirty = true; } if (!string.IsNullOrEmpty(templateList.DocumentTemplate)) { if (existingList.DocumentTemplateUrl != parser.ParseString(templateList.DocumentTemplate)) { existingList.DocumentTemplateUrl = parser.ParseString(templateList.DocumentTemplate); isDirty = true; } } if (!string.IsNullOrEmpty(templateList.Description) && templateList.Description != existingList.Description) { existingList.Description = templateList.Description; isDirty = true; } if (templateList.Hidden != existingList.Hidden) { existingList.Hidden = templateList.Hidden; isDirty = true; } if (templateList.OnQuickLaunch != existingList.OnQuickLaunch) { existingList.OnQuickLaunch = templateList.OnQuickLaunch; isDirty = true; } if (templateList.ContentTypesEnabled != existingList.ContentTypesEnabled) { existingList.ContentTypesEnabled = templateList.ContentTypesEnabled; isDirty = true; } if (existingList.BaseTemplate != (int)ListTemplateType.Survey && existingList.BaseTemplate != (int)ListTemplateType.DocumentLibrary) { // https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.splist.enableattachments.aspx // The EnableAttachments property does not apply to any list that has a base type of Survey or DocumentLibrary. // If you set this property to true for either type of list, it throws an SPException. if (templateList.EnableAttachments != existingList.EnableAttachments) { existingList.EnableAttachments = templateList.EnableAttachments; isDirty = true; } } if (existingList.BaseTemplate != (int)ListTemplateType.DiscussionBoard) { if (templateList.EnableFolderCreation != existingList.EnableFolderCreation) { existingList.EnableFolderCreation = templateList.EnableFolderCreation; isDirty = true; } } if (templateList.EnableVersioning) { if (existingList.EnableVersioning != templateList.EnableVersioning) { existingList.EnableVersioning = templateList.EnableVersioning; isDirty = true; } #if !CLIENTSDKV15 if (existingList.IsObjectPropertyInstantiated("MajorVersionLimit") && existingList.MajorVersionLimit != templateList.MaxVersionLimit) { existingList.MajorVersionLimit = templateList.MaxVersionLimit; isDirty = true; } #endif if (existingList.BaseTemplate == (int)ListTemplateType.DocumentLibrary) { // Only supported on Document Libraries if (templateList.EnableMinorVersions != existingList.EnableMinorVersions) { existingList.EnableMinorVersions = templateList.EnableMinorVersions; isDirty = true; } if ((DraftVisibilityType)templateList.DraftVersionVisibility != existingList.DraftVersionVisibility) { existingList.DraftVersionVisibility = (DraftVisibilityType)templateList.DraftVersionVisibility; isDirty = true; } if (templateList.EnableMinorVersions) { if (templateList.MinorVersionLimit != existingList.MajorWithMinorVersionsLimit) { existingList.MajorWithMinorVersionsLimit = templateList.MinorVersionLimit; } if (DraftVisibilityType.Approver == (DraftVisibilityType)templateList.DraftVersionVisibility) { if (templateList.EnableModeration) { if ((DraftVisibilityType)templateList.DraftVersionVisibility != existingList.DraftVersionVisibility) { existingList.DraftVersionVisibility = (DraftVisibilityType)templateList.DraftVersionVisibility; isDirty = true; } } } else { if ((DraftVisibilityType)templateList.DraftVersionVisibility != existingList.DraftVersionVisibility) { existingList.DraftVersionVisibility = (DraftVisibilityType)templateList.DraftVersionVisibility; isDirty = true; } } } } } if (isDirty) { existingList.Update(); web.Context.ExecuteQueryRetry(); } if (existingList.ContentTypesEnabled) { // Check if we need to add a content type var existingContentTypes = existingList.ContentTypes; web.Context.Load(existingContentTypes, cts => cts.Include(ct => ct.StringId)); web.Context.ExecuteQueryRetry(); var bindingsToAdd = templateList.ContentTypeBindings.Where(ctb => existingContentTypes.All(ct => !ctb.ContentTypeId.Equals(ct.StringId, StringComparison.InvariantCultureIgnoreCase))).ToList(); var defaultCtBinding = templateList.ContentTypeBindings.FirstOrDefault(ctb => ctb.Default == true); foreach (var ctb in bindingsToAdd) { existingList.AddContentTypeToListById(ctb.ContentTypeId, searchContentTypeInSiteHierarchy: true); } // default ContentTypeBinding should be set last because // list extension .SetDefaultContentTypeToList() re-sets // the list.RootFolder UniqueContentTypeOrder property // which may cause missing CTs from the "New Button" if (defaultCtBinding != null) { existingList.SetDefaultContentTypeToList(defaultCtBinding.ContentTypeId); } } if (templateList.Security != null) { existingList.SetSecurity(parser, templateList.Security); } return Tuple.Create(existingList, parser); } else { scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ListInstances_List__0____1____2___exists_but_is_of_a_different_type__Skipping_list_, templateList.Title, templateList.Url, existingList.Id); WriteWarning(string.Format(CoreResources.Provisioning_ObjectHandlers_ListInstances_List__0____1____2___exists_but_is_of_a_different_type__Skipping_list_, templateList.Title, templateList.Url, existingList.Id), ProvisioningMessageType.Warning); return null; } }
private Tuple<List, TokenParser> UpdateList(Web web, List existingList, ListInstance templateList, TokenParser parser, PnPMonitoredScope scope, bool isNoScriptSite = false) { web.Context.Load(existingList, l => l.Title, l => l.Description, l => l.OnQuickLaunch, l => l.Hidden, l => l.ContentTypesEnabled, l => l.EnableAttachments, l => l.EnableVersioning, l => l.EnableFolderCreation, l => l.EnableModeration, l => l.EnableMinorVersions, l => l.ForceCheckout, l => l.DraftVersionVisibility, l => l.Views, l => l.DocumentTemplateUrl, l => l.RootFolder, l => l.BaseType, l => l.BaseTemplate #if !SP2013 , l => l.MajorWithMinorVersionsLimit , l => l.MajorVersionLimit #endif ); web.Context.ExecuteQueryRetry(); if (existingList.BaseTemplate == templateList.TemplateType) { var isDirty = false; if (parser.ParseString(templateList.Title) != existingList.Title) { var oldTitle = existingList.Title; existingList.Title = parser.ParseString(templateList.Title); if (!oldTitle.Equals(existingList.Title, StringComparison.OrdinalIgnoreCase)) { parser.AddToken(new ListIdToken(web, existingList.Title, existingList.Id)); parser.AddToken(new ListUrlToken(web, existingList.Title, existingList.RootFolder.ServerRelativeUrl.Substring(web.ServerRelativeUrl.Length + 1))); } isDirty = true; } if (!string.IsNullOrEmpty(templateList.DocumentTemplate)) { if (existingList.DocumentTemplateUrl != parser.ParseString(templateList.DocumentTemplate)) { existingList.DocumentTemplateUrl = parser.ParseString(templateList.DocumentTemplate); isDirty = true; } } if (!string.IsNullOrEmpty(templateList.Description) && templateList.Description != existingList.Description) { existingList.Description = templateList.Description; isDirty = true; } if (templateList.Hidden != existingList.Hidden) { existingList.Hidden = templateList.Hidden; isDirty = true; } if (templateList.OnQuickLaunch != existingList.OnQuickLaunch) { existingList.OnQuickLaunch = templateList.OnQuickLaunch; isDirty = true; } if (existingList.BaseTemplate != (int)ListTemplateType.Survey && templateList.ContentTypesEnabled != existingList.ContentTypesEnabled) { existingList.ContentTypesEnabled = templateList.ContentTypesEnabled; isDirty = true; } if (existingList.BaseTemplate != (int)ListTemplateType.Survey && existingList.BaseTemplate != (int)ListTemplateType.DocumentLibrary && existingList.BaseTemplate != (int)ListTemplateType.PictureLibrary) { // https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.splist.enableattachments.aspx // The EnableAttachments property does not apply to any list that has a base type of Survey, DocumentLibrary or PictureLibrary. // If you set this property to true for either type of list, it throws an SPException. if (templateList.EnableAttachments != existingList.EnableAttachments) { existingList.EnableAttachments = templateList.EnableAttachments; isDirty = true; } } if (existingList.BaseTemplate != (int)ListTemplateType.DiscussionBoard) { if (templateList.EnableFolderCreation != existingList.EnableFolderCreation) { existingList.EnableFolderCreation = templateList.EnableFolderCreation; isDirty = true; } } #if !SP2013 if (templateList.Title.ContainsResourceToken()) { if (existingList.TitleResource.SetUserResourceValue(templateList.Title, parser)) { isDirty = true; } } #endif if (existingList.EnableModeration != templateList.EnableModeration) { existingList.EnableModeration = templateList.EnableModeration; isDirty = true; } if (templateList.ForceCheckout != existingList.ForceCheckout) { existingList.ForceCheckout = templateList.ForceCheckout; isDirty = true; } if (templateList.EnableVersioning) { if (existingList.EnableVersioning != templateList.EnableVersioning) { existingList.EnableVersioning = templateList.EnableVersioning; isDirty = true; } #if !SP2013 if (existingList.MajorVersionLimit != templateList.MaxVersionLimit) { existingList.MajorVersionLimit = templateList.MaxVersionLimit; isDirty = true; } #endif if (existingList.BaseType == BaseType.DocumentLibrary) { // Only supported on Document Libraries if (templateList.EnableMinorVersions != existingList.EnableMinorVersions) { existingList.EnableMinorVersions = templateList.EnableMinorVersions; isDirty = true; } if ((DraftVisibilityType)templateList.DraftVersionVisibility != existingList.DraftVersionVisibility) { existingList.DraftVersionVisibility = (DraftVisibilityType)templateList.DraftVersionVisibility; isDirty = true; } if (templateList.EnableMinorVersions) { if (templateList.MinorVersionLimit != existingList.MajorWithMinorVersionsLimit) { existingList.MajorWithMinorVersionsLimit = templateList.MinorVersionLimit; } if (DraftVisibilityType.Approver == (DraftVisibilityType)templateList.DraftVersionVisibility) { if (templateList.EnableModeration) { if ((DraftVisibilityType)templateList.DraftVersionVisibility != existingList.DraftVersionVisibility) { existingList.DraftVersionVisibility = (DraftVisibilityType)templateList.DraftVersionVisibility; isDirty = true; } } } else { if ((DraftVisibilityType)templateList.DraftVersionVisibility != existingList.DraftVersionVisibility) { existingList.DraftVersionVisibility = (DraftVisibilityType)templateList.DraftVersionVisibility; isDirty = true; } } } } } else { if (existingList.EnableVersioning != templateList.EnableVersioning) { existingList.EnableVersioning = templateList.EnableVersioning; isDirty = true; } } if (isDirty) { existingList.Update(); web.Context.ExecuteQueryRetry(); isDirty = false; } #region UserCustomActions if (!isNoScriptSite) { // Add any UserCustomActions var existingUserCustomActions = existingList.UserCustomActions; web.Context.Load(existingUserCustomActions); web.Context.ExecuteQueryRetry(); foreach (CustomAction userCustomAction in templateList.UserCustomActions) { // Check for existing custom actions before adding (compare by custom action name) if (!existingUserCustomActions.AsEnumerable().Any(uca => uca.Name == userCustomAction.Name)) { CreateListCustomAction(existingList, parser, userCustomAction); isDirty = true; } else { var existingCustomAction = existingUserCustomActions.AsEnumerable().FirstOrDefault(uca => uca.Name == userCustomAction.Name); if (existingCustomAction != null) { isDirty = true; // If the custom action already exists if (userCustomAction.Remove) { // And if we need to remove it, we simply delete it existingCustomAction.DeleteObject(); } else { // Otherwise we update it, and before we force the target // registration type and ID to avoid issues userCustomAction.RegistrationType = UserCustomActionRegistrationType.List; userCustomAction.RegistrationId = existingList.Id.ToString("B").ToUpper(); ObjectCustomActions.UpdateCustomAction(parser, scope, userCustomAction, existingCustomAction); // Blank out these values again to avoid inconsistent domain model data userCustomAction.RegistrationType = UserCustomActionRegistrationType.None; userCustomAction.RegistrationId = null; } } } } if (isDirty) { existingList.Update(); web.Context.ExecuteQueryRetry(); isDirty = false; } } else { scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ListInstances_SkipAddingOrUpdatingCustomActions); } #endregion if (existingList.ContentTypesEnabled) { // Check if we need to add a content type var existingContentTypes = existingList.ContentTypes; web.Context.Load(existingContentTypes, cts => cts.Include(ct => ct.StringId)); web.Context.ExecuteQueryRetry(); var bindingsToAdd = templateList.ContentTypeBindings.Where(ctb => existingContentTypes.All(ct => !ctb.ContentTypeId.Equals(ct.StringId, StringComparison.InvariantCultureIgnoreCase))).ToList(); var defaultCtBinding = templateList.ContentTypeBindings.FirstOrDefault(ctb => ctb.Default == true); var currentDefaultContentTypeId = existingContentTypes.First().StringId; foreach (var ctb in bindingsToAdd) { var tempCT = web.GetContentTypeById(ctb.ContentTypeId, searchInSiteHierarchy: true); if (tempCT != null) { // Get the name of the existing CT var name = tempCT.EnsureProperty(ct => ct.Name); // If the CT does not exist in the target list, and we don't have to remove it if (!existingList.ContentTypeExistsByName(name) && !ctb.Remove) { existingList.AddContentTypeToListById(ctb.ContentTypeId, searchContentTypeInSiteHierarchy: true); } // Else if the CT exists in the target list, and we have to remove it else if (existingList.ContentTypeExistsByName(name) && ctb.Remove) { // Then remove it from the target list existingList.RemoveContentTypeByName(name); } } } // default ContentTypeBinding should be set last because // list extension .SetDefaultContentTypeToList() re-sets // the list.RootFolder UniqueContentTypeOrder property // which may cause missing CTs from the "New Button" if (defaultCtBinding != null) { // Only update the defualt contenttype when we detect a change in default value if (!currentDefaultContentTypeId.Equals(defaultCtBinding.ContentTypeId, StringComparison.InvariantCultureIgnoreCase)) { existingList.SetDefaultContentTypeToList(defaultCtBinding.ContentTypeId); } } } if (templateList.Security != null) { existingList.SetSecurity(parser, templateList.Security); } return Tuple.Create(existingList, parser); } else { scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ListInstances_List__0____1____2___exists_but_is_of_a_different_type__Skipping_list_, templateList.Title, templateList.Url, existingList.Id); WriteWarning(string.Format(CoreResources.Provisioning_ObjectHandlers_ListInstances_List__0____1____2___exists_but_is_of_a_different_type__Skipping_list_, templateList.Title, templateList.Url, existingList.Id), ProvisioningMessageType.Warning); return null; } }
private Tuple<List, TokenParser> UpdateList(Web web, List existingList, ListInstance templateList, TokenParser parser, PnPMonitoredScope scope) { try { web.Context.Load(existingList, l => l.Forms); web.Context.ExecuteQueryRetry(); } catch (Exception ex) { scope.LogDebug("Loading forms on list {0} failed. {1}. {2}", existingList.Title, ex.Message, ex.StackTrace); } if (existingList.BaseTemplate == templateList.TemplateType) { var isDirty = false; if (parser.ParseString(templateList.Title) != existingList.Title) { var oldTitle = existingList.Title; existingList.Title = parser.ParseString(templateList.Title); if (!oldTitle.Equals(existingList.Title, StringComparison.OrdinalIgnoreCase)) { parser.AddToken(new ListIdToken(web, existingList.Title, existingList.Id)); parser.AddToken(new ListUrlToken(web, existingList.Title, existingList.RootFolder.ServerRelativeUrl.Substring(web.ServerRelativeUrl.Length + (web.ServerRelativeUrl == "/" ? 0 : 1)))); } isDirty = true; } if (!string.IsNullOrEmpty(templateList.DocumentTemplate)) { if (existingList.DocumentTemplateUrl != parser.ParseString(templateList.DocumentTemplate)) { var documentTemplateUrl = parser.ParseString(templateList.DocumentTemplate); var templateFolderUrl = documentTemplateUrl.Substring(0, documentTemplateUrl.LastIndexOf('/')); var templateForder = web.GetFolderByServerRelativeUrl(templateFolderUrl); var templateName = documentTemplateUrl.Substring(documentTemplateUrl.LastIndexOf('/') + 1); var templateStream = new MemoryStream(Encoding.UTF8.GetBytes(templateList.DocumentTemplateContent)); templateForder.UploadFile(templateName, templateStream, true); existingList.DocumentTemplateUrl = documentTemplateUrl; isDirty = true; } } if (!string.IsNullOrEmpty(templateList.Description) && templateList.Description != existingList.Description) { existingList.Description = templateList.Description; isDirty = true; } if (templateList.Hidden != existingList.Hidden) { existingList.Hidden = templateList.Hidden; isDirty = true; } if (templateList.OnQuickLaunch != existingList.OnQuickLaunch) { existingList.OnQuickLaunch = templateList.OnQuickLaunch; isDirty = true; } if (existingList.BaseTemplate != (int)ListTemplateType.Survey && templateList.ContentTypesEnabled != existingList.ContentTypesEnabled) { existingList.ContentTypesEnabled = templateList.ContentTypesEnabled; isDirty = true; } if (existingList.BaseTemplate != (int)ListTemplateType.Survey && existingList.BaseTemplate != (int)ListTemplateType.DocumentLibrary) { // https://msdn.microsoft.com/EN-US/library/microsoft.sharepoint.splist.enableattachments.aspx // The EnableAttachments property does not apply to any list that has a base type of Survey or DocumentLibrary. // If you set this property to true for either type of list, it throws an SPException. if (templateList.EnableAttachments != existingList.EnableAttachments) { existingList.EnableAttachments = templateList.EnableAttachments; isDirty = true; } } if (existingList.BaseTemplate != (int)ListTemplateType.DiscussionBoard) { if (templateList.EnableFolderCreation != existingList.EnableFolderCreation) { existingList.EnableFolderCreation = templateList.EnableFolderCreation; isDirty = true; } } #if !CLIENTSDKV15 if(templateList.Title.ContainsResourceToken()) { if(existingList.TitleResource.SetUserResourceValue(templateList.Title, parser)) { isDirty = true; } } #endif if (templateList.EnableVersioning) { if (existingList.EnableVersioning != templateList.EnableVersioning) { existingList.EnableVersioning = templateList.EnableVersioning; isDirty = true; } #if !CLIENTSDKV15 if (existingList.IsObjectPropertyInstantiated("MajorVersionLimit") && existingList.MajorVersionLimit != templateList.MaxVersionLimit) { existingList.MajorVersionLimit = templateList.MaxVersionLimit; isDirty = true; } #endif if (existingList.BaseType == BaseType.DocumentLibrary) { // Only supported on Document Libraries if (templateList.EnableMinorVersions != existingList.EnableMinorVersions) { existingList.EnableMinorVersions = templateList.EnableMinorVersions; isDirty = true; } if ((DraftVisibilityType)templateList.DraftVersionVisibility != existingList.DraftVersionVisibility) { existingList.DraftVersionVisibility = (DraftVisibilityType)templateList.DraftVersionVisibility; isDirty = true; } if (templateList.EnableMinorVersions) { if (templateList.MinorVersionLimit != existingList.MajorWithMinorVersionsLimit) { existingList.MajorWithMinorVersionsLimit = templateList.MinorVersionLimit; } if (DraftVisibilityType.Approver == (DraftVisibilityType)templateList.DraftVersionVisibility) { if (templateList.EnableModeration) { if ((DraftVisibilityType)templateList.DraftVersionVisibility != existingList.DraftVersionVisibility) { existingList.DraftVersionVisibility = (DraftVisibilityType)templateList.DraftVersionVisibility; isDirty = true; } } } else { if ((DraftVisibilityType)templateList.DraftVersionVisibility != existingList.DraftVersionVisibility) { existingList.DraftVersionVisibility = (DraftVisibilityType)templateList.DraftVersionVisibility; isDirty = true; } } } } } if (isDirty) { existingList.Update(); web.Context.ExecuteQueryRetry(); } if (existingList.ContentTypesEnabled) { // Check if we need to add a content type var existingContentTypes = existingList.ContentTypes; var bindingsToAdd = templateList.ContentTypeBindings.Where(ctb => existingContentTypes.All(ct => !ctb.ContentTypeId.Equals(ct.StringId, StringComparison.InvariantCultureIgnoreCase))).ToList(); var defaultCtBinding = templateList.ContentTypeBindings.FirstOrDefault(ctb => ctb.Default == true); var bindingAddedToList = false; foreach (var ctb in bindingsToAdd) { // Added a check so that if no bindings were actually added then the SetDefaultContentTypeToList method will not be executed // This is to address a specific scenario when OOTB PWA lists can not be updated as they are centrally managed var contentType = web.AvailableContentTypes.AsEnumerable().FirstOrDefault(item => item.Id.StringValue.Equals(ctb.ContentTypeId, StringComparison.OrdinalIgnoreCase)); if (contentType != null) { // Check if CT is already available var name = contentType.EnsureProperty(ct => ct.Name); if (!existingList.ContentTypes.AsEnumerable().Any(item => item.Name == name)) { if (existingList.ContentTypesEnabled == false) { existingList.ContentTypesEnabled = true; existingList.Update(); existingList.Context.ExecuteQueryRetry(); } existingList.ContentTypes.AddExistingContentType(contentType); bindingAddedToList = true; } } } if (bindingAddedToList) { web.Context.ExecuteQueryRetry(); } // default ContentTypeBinding should be set last because // list extension .SetDefaultContentTypeToList() re-sets // the list.RootFolder UniqueContentTypeOrder property // which may cause missing CTs from the "New Button" if (defaultCtBinding != null && bindingAddedToList) { existingList.SetDefaultContentTypeToList(defaultCtBinding.ContentTypeId); } } if (templateList.Security != null) { try { existingList.SetSecurity(parser, templateList.Security); } catch (Exception ex) { scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstances_Updating_list__0__failed___1_____2_, templateList.Title, ex.Message, ex.StackTrace); } } return Tuple.Create(existingList, parser); } else { scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ListInstances_List__0____1____2___exists_but_is_of_a_different_type__Skipping_list_, templateList.Title, templateList.Url, existingList.Id); WriteWarning(string.Format(CoreResources.Provisioning_ObjectHandlers_ListInstances_List__0____1____2___exists_but_is_of_a_different_type__Skipping_list_, templateList.Title, templateList.Url, existingList.Id), ProvisioningMessageType.Warning); return null; } }