public static void SetUpCalculator(InstallInfo info) { using (ClientContext ctx = ContextHelper.GetAppOnlyContext(info.HostUrl)) { Console.WriteLine("Connected to sharepoint"); string ListName = "Calculator"; List calculatorList = null; Console.WriteLine("Creating LIst if not exists"); if (!ctx.Web.ListExists(ListName)) { calculatorList = ctx.Web.CreateList(ListTemplateType.GenericList, ListName, false); } else { calculatorList = ctx.Web.GetListByTitle(ListName); } Console.WriteLine("Setting up fields and content types"); string pathToXML = AppDomain.CurrentDomain.BaseDirectory + @"\SPContent\Fields.xml"; ctx.Site.RootWeb.CreateFieldsFromXMLFile(pathToXML); ctx.Site.RootWeb.CreateContentTypeFromXMLFile(pathToXML); if (!calculatorList.ContentTypeExistsByName("Calculator")) { calculatorList.AddContentTypeToListById("0x01002B999C32793148858FE620188A8353AC", true, true); calculatorList.RemoveContentTypeByName("Item"); } SetUpRemoteEventReciver(ctx, info.AppUrlServiceEndPoint); } }
public static void SetUpContent(ClientContext ctx) { List issueList = ctx.Web.CreateList(ListTemplateType.GenericList, "Important Issues", false, true, "lists/importantissues", true); string pathToXML = AppDomain.CurrentDomain.BaseDirectory + "Important Issue Content.xml"; XDocument doc = XDocument.Load(pathToXML); List <XElement> LookupElements = doc.Root.Elements("Field").Where(e => e.Attribute("Type").Value == "Lookup").ToList(); foreach (XElement element in LookupElements) { string listUrl = element.Attribute("List").Value; List list = ctx.Web.GetListByUrl(listUrl); element.Attribute("List").Value = list.Id.ToString(); } ctx.Web.CreateFieldsFromXMLString(doc.ToString()); ctx.Web.CreateContentTypeFromXMLString(doc.ToString()); issueList.AddContentTypeToListById("0x01002312F6C11D9A41F3A3532FE936842C34", true); issueList.RemoveContentTypeByName("Item"); }
public static void SetUpCalculator(ClientContext ctx) { string ListName = "Calculator"; List calculatorList = null; if (!ctx.Web.ListExists(ListName)) { calculatorList = ctx.Web.CreateList(ListTemplateType.GenericList, ListName, false); } else { calculatorList = ctx.Web.GetListByTitle(ListName); } string pathToXML = AppDomain.CurrentDomain.BaseDirectory + @"\SPContent\Fields.xml"; ctx.Site.RootWeb.CreateFieldsFromXMLFile(pathToXML); ctx.Site.RootWeb.CreateContentTypeFromXMLFile(pathToXML); calculatorList.AddContentTypeToListById("0x01002B999C32793148858FE620188A8353AC", true, true); calculatorList.RemoveContentTypeByName("Item"); }
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) { 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; } }
public static void MyFirstContentType(ClientContext ctx) { Web rootWeb = ctx.Site.RootWeb; //string carsID = "0x01001703716880F147E38BD026AA8DDD14D5"; //if (rootWeb.ContentTypeExistsById(carsID)) //{ // rootWeb.DeleteContentTypeById(carsID); //} //rootWeb.CreateContentType("Cars", carsID, "Tims Columns"); //FieldCreationInformation brandField = new FieldCreationInformation(FieldType.Text); //brandField.DisplayName = "Brand"; //brandField.Id = new Guid("{F4D37EFA-3CD5-4426-8F49-D2F48F841653}"); //brandField.InternalName = "CMS_Brand"; //brandField.Group = "Tims Columns"; //if (rootWeb.FieldExistsById(brandField.Id)) //{ // rootWeb.RemoveFieldById(brandField.Id.ToString()); //} //rootWeb.CreateField(brandField); //rootWeb.AddFieldToContentTypeById(carsID, "{F4D37EFA-3CD5-4426-8F49-D2F48F841653}", false); //FieldCreationInformation yearField = new FieldCreationInformation(FieldType.Number); //yearField.DisplayName = "Year"; //yearField.Id = new Guid("{F1CE6B6C-652B-470B-8299-5081E16A8CB2}"); //yearField.InternalName = "CMS_Year"; //yearField.Group = "Tims Columns"; //if (rootWeb.FieldExistsById(yearField.Id)) //{ // rootWeb.RemoveFieldById(yearField.Id.ToString()); //} //rootWeb.CreateField(yearField); //rootWeb.AddFieldToContentTypeById(carsID, "{F1CE6B6C-652B-470B-8299-5081E16A8CB2}", false); //FieldCreationInformation colorField = new FieldCreationInformation(FieldType.Choice); //colorField.DisplayName = "Color"; //colorField.Id = new Guid("{9EC39186-A1E6-4DA3-8343-84A7C7137714}"); //colorField.InternalName = "CMS_Color"; //colorField.Group = "Tims Columns"; //if (rootWeb.FieldExistsById(colorField.Id)) //{ // rootWeb.RemoveFieldById(colorField.Id.ToString()); //} //var fc = rootWeb.CreateField<FieldChoice>(colorField); //fc.Choices = new string[] { "red", "green", "blue" }; ////rootWeb.CreateField(colorField); //fc.Update(); //ctx.ExecuteQuery(); //rootWeb.AddFieldToContentTypeById(carsID, "{9EC39186-A1E6-4DA3-8343-84A7C7137714}", false); //List list = rootWeb.GetListByTitle("Tims List From Pnp"); //list.AddContentTypeToList(rootWeb.GetContentTypeById(carsID), true); //for (int i = 0; i < 5; i++) //{ // list.CreateDocument("dokument " + i, list.RootFolder, DocumentTemplateType.Word); // Console.WriteLine("done"+i); //} //list.DefaultView.ViewFields.Add("CMS_Brand"); //list.DefaultView.ViewFields.Add("CMS_Year"); //list.DefaultView.ViewFields.Add("CMS_Color"); //list.DefaultView.Update(); //list.Update(); //ctx.ExecuteQuery(); //this is another way to do it using xml //rootWeb.Fields.AddFieldAsXml("<field DisplayName = "brand"></fields>) string booksID = "0x01003DCFC9B08E1E4DD18AB8BD9053D7F49E"; if (rootWeb.ContentTypeExistsById(booksID)) { rootWeb.DeleteContentTypeById(booksID); } rootWeb.CreateContentType("Books", booksID, "Tims Columns"); FieldCreationInformation bookType = new FieldCreationInformation(FieldType.Choice); bookType.DisplayName = "Book Type"; bookType.Id = new Guid("{F0C6D85D-C4DD-48F2-806E-5794ABDCAAB0}"); bookType.InternalName = "CMS_BookType"; bookType.Group = "Tims Columns"; var fcs = rootWeb.CreateField <FieldChoice>(bookType); fcs.Choices = new string[] { "Large", "Medium", "Small" }; fcs.Update(); ctx.ExecuteQuery(); FieldCreationInformation author = new FieldCreationInformation(FieldType.Text); author.DisplayName = "Author"; author.Id = new Guid("{A95D3D0B-F076-4675-A99A-7D8EED002481}"); author.InternalName = "CMS_Author"; author.Group = "Tims Columns"; FieldCreationInformation dateReleased = new FieldCreationInformation(FieldType.DateTime); dateReleased.DisplayName = "Date released"; dateReleased.Id = new Guid("{7C81173A-141C-4008-B2E6-A8763F0850DF}"); dateReleased.InternalName = "CMS_dateReleased"; dateReleased.Group = "Tims Columns"; FieldCreationInformation description = new FieldCreationInformation(FieldType.Note); description.DisplayName = "Description"; description.Id = new Guid("{B65E6297-0182-4CA8-A7DE-45E05FE61086}"); description.InternalName = "CMS_description"; description.Group = "Tims Columns"; //rootWeb.CreateField(bookType); rootWeb.CreateField(author); rootWeb.CreateField(dateReleased); rootWeb.CreateField(description); rootWeb.AddFieldToContentTypeById(booksID, "{F0C6D85D-C4DD-48F2-806E-5794ABDCAAB0}", false); rootWeb.AddFieldToContentTypeById(booksID, "{A95D3D0B-F076-4675-A99A-7D8EED002481}", false); rootWeb.AddFieldToContentTypeById(booksID, "{7C81173A-141C-4008-B2E6-A8763F0850DF}", false); rootWeb.AddFieldToContentTypeById(booksID, "{B65E6297-0182-4CA8-A7DE-45E05FE61086}", false); List list = rootWeb.CreateList(ListTemplateType.GenericList, "books", false, true, "TimsGenericList", true); list.AddContentTypeToListById(booksID, true); }
private List UpdateList(Web web, List existingList, ListInstance templateList) { 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 #if !CLIENTSDKV15 , l => l.MajorWithMinorVersionsLimit #endif ); web.Context.ExecuteQueryRetry(); if (existingList.BaseTemplate == templateList.TemplateType) { var isDirty = false; if (templateList.Title != existingList.Title) { existingList.Title = templateList.Title; isDirty = true; } if (!string.IsNullOrEmpty(templateList.DocumentTemplate)) { if (existingList.DocumentTemplateUrl != templateList.DocumentTemplate.ToParsedString()) { existingList.DocumentTemplateUrl = templateList.DocumentTemplate.ToParsedString(); 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 (existingList.MajorVersionLimit != templateList.MaxVersionLimit) { existingList.MajorVersionLimit = templateList.MaxVersionLimit; isDirty = true; } 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); } } return(existingList); } else { WriteWarning(string.Format("List {0} ({1}, {2}) exists but is of a different type. Skipping list.", templateList.Title, templateList.Url, existingList.Id), ProvisioningMessageType.Warning); return(null); } }
public static void CreateCV(ClientContext ctx) { string cVCT = "0x010100A959F697950047DF80D85119D99F8CA7"; Web web = ctx.Site.RootWeb; if (!web.ContentTypeExistsById(cVCT)) { web.CreateContentType("CV", cVCT, "Davids ContentType"); } string picFieldId = "{98A1C95C-AA0F-4D2C-92C8-5407594C440F}"; if (!web.FieldExistsById(new Guid(picFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.URL); info.Id = picFieldId.ToGuid(); info.InternalName = "DAV_Pic"; info.DisplayName = "Picture"; info.Group = "Tims Columns"; FieldUrl picfield = web.CreateField <FieldUrl>(info); picfield.DisplayFormat = UrlFieldFormatType.Image; picfield.Update(); ctx.ExecuteQuery(); } string userFieldId = "{B0C1EFC4-189E-4626-A1DC-1CCC4693C097}"; if (!web.FieldExistsById(new Guid(userFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.User); info.Id = userFieldId.ToGuid(); info.InternalName = "DAV_User"; info.DisplayName = "User"; info.Group = "Tims Columns"; FieldUser userfield = web.CreateField <FieldUser>(info); ctx.ExecuteQuery(); } string activeFieldId = "{2CB24A28-3F5B-49AE-9F54-5FD8747DBF19}"; if (!web.FieldExistsById(new Guid(activeFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Boolean); info.Id = activeFieldId.ToGuid(); info.InternalName = "DAV_Active"; info.DisplayName = "Active"; info.Group = "Tims Columns"; web.CreateField(info); } web.AddFieldToContentTypeById(cVCT, picFieldId); web.AddFieldToContentTypeById(cVCT, userFieldId); web.AddFieldToContentTypeById(cVCT, activeFieldId); if (!web.ListExists("CVs")) { List list = web.CreateList(ListTemplateType.DocumentLibrary, "CVs", true, enableContentTypes: true); list.AddContentTypeToListById(cVCT); } List CVList = web.GetListByTitle("CVs"); FileCreationInformation fileinfo = new FileCreationInformation(); System.IO.FileStream fileStream = System.IO.File.OpenRead(@"C:\Users\timha\source\repos\Officedeveloper1\ContentTypesAndFields\ContentTypesAndFields\TextFile1.txt"); fileinfo.Content = ReadFully(fileStream); fileinfo.Url = "file1.txt"; Microsoft.SharePoint.Client.File files = CVList.RootFolder.Files.Add(fileinfo); ctx.ExecuteQuery(); User user = web.EnsureUser("*****@*****.**"); ctx.Load(user); ctx.ExecuteQuery(); ListItem item = files.ListItemAllFields; item["Title"] = "Tim"; item["ContentTypeId"] = cVCT; FieldUrlValue picvalue = new FieldUrlValue(); picvalue.Description = "Tim"; picvalue.Url = "https://images.pexels.com/photos/104827/cat-pet-animal-domestic-104827.jpeg?w=940&h=650&auto=compress&cs=tinysrgb"; item["DAV_Pic"] = picvalue; item["DAV_User"] = user.Id; item["DAV_Active"] = true; item.Update(); ctx.ExecuteQuery(); }
public static void CreateBookCT(ClientContext ctx) { string bookCT = "0x01000E870749A9444905BB8A362E475B0798"; Web web = ctx.Site.RootWeb; //web.GetListByTitle("Books2").DeleteObject(); //ctx.ExecuteQuery(); //web.DeleteContentTypeById(bookCT); if (!web.ContentTypeExistsById(bookCT)) { web.CreateContentType("David Books", bookCT, "Davids ContentType"); } string bookTypeFieldId = "{DBB24705-0DEA-4C4F-8C2A-95CB6F0DE25E}"; if (!web.FieldExistsById(new Guid(bookTypeFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Choice); info.Id = bookTypeFieldId.ToGuid(); info.InternalName = "DAV_BookType"; info.DisplayName = "Book Type"; info.Group = "Tims Columns"; FieldChoice field = web.CreateField <FieldChoice>(info); field.Choices = new string[] { "Romance", "Drama", "Horror", "Thriller" }; field.Update(); ctx.ExecuteQuery(); } string authorFieldId = "{D6996667-0BEA-4C9F-9904-DEB21CC5AA84}"; if (!web.FieldExistsById(new Guid(authorFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Text); info.Id = authorFieldId.ToGuid(); info.InternalName = "DAV_Author"; info.DisplayName = "Author"; info.Group = "Tims Columns"; Field field = web.CreateField(info); } string releaseDateFieldId = "{84716863-06CA-4D31-BAA0-7D099FC501E7}"; if (!web.FieldExistsById(new Guid(releaseDateFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.DateTime); info.Id = releaseDateFieldId.ToGuid(); info.InternalName = "DAV_Realesedate"; info.DisplayName = "ReleaseDate"; info.Group = "Tims Columns"; FieldDateTime field = web.CreateField <FieldDateTime>(info); field.DisplayFormat = DateTimeFieldFormatType.DateOnly; field.Update(); ctx.ExecuteQuery(); } string descriptionDateFieldId = "{4BD3F599-4D5C-412D-8431-6ECD36AEB015}"; // web.RemoveFieldById(descriptionDateFieldId); if (!web.FieldExistsById(new Guid(descriptionDateFieldId))) { FieldCreationInformation info = new FieldCreationInformation(FieldType.Note); info.Id = descriptionDateFieldId.ToGuid(); info.InternalName = "DAV_description"; info.DisplayName = "Description"; info.Group = "Tims Columns"; info.Required = true; FieldMultiLineText field = web.CreateField <FieldMultiLineText>(info); field.RichText = true; field.NumberOfLines = 10; field.AllowHyperlink = true; field.Update(); ctx.ExecuteQuery(); } web.AddFieldToContentTypeById(bookCT, bookTypeFieldId); web.AddFieldToContentTypeById(bookCT, authorFieldId); web.AddFieldToContentTypeById(bookCT, releaseDateFieldId); web.AddFieldToContentTypeById(bookCT, descriptionDateFieldId, true); if (!web.ListExists("Books2")) { List list = web.CreateList(ListTemplateType.GenericList, "Books2", false, urlPath: "lists/books2", enableContentTypes: true); list.AddContentTypeToListById(bookCT, true); View listView = list.DefaultView; listView.ViewFields.Add("DAV_BookType"); listView.ViewFields.Add("DAV_Author"); listView.ViewFields.Add("DAV_Realesedate"); listView.ViewFields.Add("DAV_description"); listView.Update(); ctx.ExecuteQueryRetry(); } List bookList = web.GetListByTitle("Books2"); ListItem item = bookList.AddItem(new ListItemCreationInformation()); item["Title"] = "MistBorn"; item["DAV_BookType"] = "Fantasy"; item["DAV_Author"] = "Brandon Sanderson"; item["DAV_Realesedate"] = DateTime.Parse("2001-02-12"); item["DAV_description"] = "This is a decription \n\n is this a new line?"; item.Update(); ctx.ExecuteQuery(); //ListItemCollection items = bookList.GetItems(CamlQuery.CreateAllItemsQuery()); //ctx.Load(items); //ctx.ExecuteQuery(); }