public override void ProvisionObjects(Web web, ProvisioningTemplate template) { TokenParser parser = new TokenParser(web); var context = web.Context as ClientContext; if (!web.IsPropertyAvailable("ServerRelativeUrl")) { context.Load(web, w => w.ServerRelativeUrl); context.ExecuteQuery(); } foreach (var file in template.Files) { var folderName = parser.Parse(file.Folder); if (folderName.ToLower().StartsWith((web.ServerRelativeUrl.ToLower()))) { folderName = folderName.Substring(web.ServerRelativeUrl.Length); } var folder = web.EnsureFolderPath(folderName); using (var stream = template.Connector.GetFileStream(file.Src)) { folder.UploadFile(file.Src, stream, true); } } }
public override void ProvisionObjects(Web web, ProvisioningTemplate template) { // if this is a sub site then we're not provisioning fields. Technically this can be done but it's not a recommended practice if (web.IsSubSite()) { return; } var parser = new TokenParser(web); var existingFields = web.Fields; web.Context.Load(existingFields, fs => fs.Include(f => f.Id)); web.Context.ExecuteQuery(); //TODO: Upgrade to SharePoint.Client v16.0 so refactoring can be done (uncomment following line when done!) //var existingFieldIds = existingFields.Select(l => l.Id).ToList(); var existingFieldIds = existingFields.AsEnumerable().Select(l => l.Id).ToList(); var fields = template.SiteFields; foreach (var field in fields) { XDocument document = XDocument.Parse(field.SchemaXml); var fieldId = document.Root.Attribute("ID").Value; if (!existingFieldIds.Contains(Guid.Parse(fieldId))) { var fieldXml = parser.Parse(field.SchemaXml); web.Fields.AddFieldAsXml(fieldXml, false, AddFieldOptions.DefaultValue); web.Context.ExecuteQuery(); } } }
public override void ProvisionObjects(Web web, ProvisioningTemplate template) { var parser = new TokenParser(web); if (!web.IsPropertyAvailable("ServerRelativeUrl")) { web.Context.Load(web, w => w.ServerRelativeUrl); web.Context.ExecuteQuery(); } web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl)); web.Context.ExecuteQuery(); //TODO: Upgrade to SharePoint.Client v16.0 so refactoring can be done (uncomment following line when done!) //var existingLists = web.Lists.Select(existingList => existingList.RootFolder.ServerRelativeUrl).ToList(); var existingLists = web.Lists.AsEnumerable().Select(existingList => existingList.RootFolder.ServerRelativeUrl).ToList(); var serverRelativeUrl = web.ServerRelativeUrl; foreach (var list in template.Lists) { if (!existingLists.Contains(UrlUtility.Combine(serverRelativeUrl, list.Url))) { var listCreate = new ListCreationInformation(); listCreate.Description = list.Description; listCreate.TemplateType = list.TemplateType; listCreate.Title = list.Title; listCreate.QuickLaunchOption = list.OnQuickLaunch ? QuickLaunchOptions.On : QuickLaunchOptions.Off; listCreate.Url = parser.Parse(list.Url); var createdList = web.Lists.Add(listCreate); createdList.EnableVersioning = list.EnableVersioning; if (!String.IsNullOrEmpty(list.DocumentTemplate)) { createdList.DocumentTemplateUrl = parser.Parse(list.DocumentTemplate); } createdList.Hidden = list.Hidden; createdList.ContentTypesEnabled = list.ContentTypesEnabled; createdList.Update(); web.Context.ExecuteQuery(); // TODO: handle 'removedefaultcontenttype' foreach (var ctBinding in list.ContentTypeBindings) { createdList.AddContentTypeToListById(ctBinding.ContentTypeID); if (ctBinding.Default) { createdList.SetDefaultContentTypeToList(ctBinding.ContentTypeID); } } if (list.Fields.Any()) { foreach (var field in list.Fields) { // Double check that the content type did not include the field before adding it in if (!createdList.FieldExistsById(field.SchemaXml.Substring(field.SchemaXml.IndexOf("ID=\"{") + 5, 36))) { var fieldXml = parser.Parse(field.SchemaXml); createdList.Fields.AddFieldAsXml(fieldXml, false, AddFieldOptions.DefaultValue); } } createdList.Update(); web.Context.ExecuteQuery(); } if (list.FieldRefs.Any()) { foreach (var fieldRef in list.FieldRefs) { var field = web.GetFieldById<Field>(fieldRef.ID); if (!createdList.FieldExistsById(fieldRef.ID)) { createdList.Fields.Add(field); } } createdList.Update(); web.Context.ExecuteQuery(); } foreach (var view in list.Views) { var viewDoc = XDocument.Parse(view.SchemaXml); var displayNameXml = viewDoc.Root.Attribute("DisplayName"); if (displayNameXml == null) { throw new ApplicationException("Invalid View element, missing a valid value for the attribute DisplayName."); } var viewTitle = displayNameXml.Value; // Type var viewTypeString = viewDoc.Root.Attribute("Type") != null ? viewDoc.Root.Attribute("Type").Value : "None"; viewTypeString = viewTypeString[0].ToString().ToUpper() + viewTypeString.Substring(1).ToLower(); var viewType = (ViewType)Enum.Parse(typeof(ViewType), viewTypeString); // Fields string[] viewFields = null; var viewFieldsElement = viewDoc.Descendants("ViewFields").FirstOrDefault(); if (viewFieldsElement != null) { viewFields = (from field in viewDoc.Descendants("ViewFields").Descendants("FieldRef") select field.Attribute("Name").Value).ToArray(); } // Default view var viewDefault = viewDoc.Root.Attribute("DefaultView") != null && Boolean.Parse(viewDoc.Root.Attribute("DefaultView").Value); // Row limit bool viewPaged = true; uint viewRowLimit = 30; var rowLimitElement = viewDoc.Descendants("RowLimit").FirstOrDefault(); if (rowLimitElement != null) { if (rowLimitElement.Attribute("Paged") != null) { viewPaged = bool.Parse(rowLimitElement.Attribute("Paged").Value); } viewRowLimit = uint.Parse(rowLimitElement.Value); } // Query var viewQuery = new StringBuilder(); foreach (var queryElement in viewDoc.Descendants("Query").Elements()) { viewQuery.Append(queryElement.ToString()); } var viewCI = new ViewCreationInformation { ViewFields = viewFields, RowLimit = viewRowLimit, Paged = viewPaged, Title = viewTitle, Query = viewQuery.ToString(), ViewTypeKind = viewType, PersonalView = false, SetAsDefaultView = viewDefault }; createdList.Views.Add(viewCI); createdList.Update(); web.Context.ExecuteQuery(); } } } }