示例#1
0
        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.ExecuteQueryRetry();
            var existingFieldIds = existingFields.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.ExecuteQueryRetry();
                }
            }
        }
示例#2
0
        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.ExecuteQueryRetry();
            }

            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);
                }
            }
        }
示例#3
0
        public override void ProvisionObjects(Web web, ProvisioningTemplate template)
        {
            if (template.ComposedLook != null &&
                !template.ComposedLook.Equals(ComposedLook.Empty))
            {
                bool        executeQueryNeeded = false;
                TokenParser parser             = new TokenParser(web);

                // Apply alternate CSS
                if (!string.IsNullOrEmpty(template.ComposedLook.AlternateCSS))
                {
                    var alternateCssUrl = parser.Parse(template.ComposedLook.AlternateCSS);
                    web.AlternateCssUrl = alternateCssUrl;
                    web.Update();
                    executeQueryNeeded = true;
                }

                // Apply Site logo
                if (!string.IsNullOrEmpty(template.ComposedLook.SiteLogo))
                {
                    var siteLogoUrl = parser.Parse(template.ComposedLook.SiteLogo);
                    web.SiteLogoUrl = siteLogoUrl;
                    web.Update();
                    executeQueryNeeded = true;
                }

                if (executeQueryNeeded)
                {
                    web.Context.ExecuteQueryRetry();
                }

                if (String.IsNullOrEmpty(template.ComposedLook.ColorFile) &&
                    String.IsNullOrEmpty(template.ComposedLook.FontFile) &&
                    String.IsNullOrEmpty(template.ComposedLook.BackgroundFile))
                {
                    // Apply OOB theme
                    web.SetComposedLookByUrl(template.ComposedLook.Name);
                }
                else
                {
                    // Apply custom theme
                    string colorFile = null;
                    if (!string.IsNullOrEmpty(template.ComposedLook.ColorFile))
                    {
                        colorFile = parser.Parse(template.ComposedLook.ColorFile);
                    }
                    string backgroundFile = null;
                    if (!string.IsNullOrEmpty(template.ComposedLook.BackgroundFile))
                    {
                        backgroundFile = parser.Parse(template.ComposedLook.BackgroundFile);
                    }
                    string fontFile = null;
                    if (!string.IsNullOrEmpty(template.ComposedLook.FontFile))
                    {
                        fontFile = parser.Parse(template.ComposedLook.FontFile);
                    }

                    string masterUrl = null;
                    if (!string.IsNullOrEmpty(template.ComposedLook.MasterPage))
                    {
                        masterUrl = parser.Parse(template.ComposedLook.MasterPage);
                    }
                    web.CreateComposedLookByUrl(template.ComposedLook.Name, colorFile, fontFile, backgroundFile, masterUrl);
                    web.SetComposedLookByUrl(template.ComposedLook.Name, colorFile, fontFile, backgroundFile, masterUrl);
                }
            }
        }
示例#4
0
        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.ExecuteQueryRetry();
            }

            web.Context.Load(web.Lists, lc => lc.IncludeWithDefaultProperties(l => l.RootFolder.ServerRelativeUrl));
            web.Context.ExecuteQueryRetry();
            var existingLists     = web.Lists.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.ExecuteQueryRetry();


                    // 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.ExecuteQueryRetry();
                    }

                    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.ExecuteQueryRetry();
                    }

                    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.ExecuteQueryRetry();
                    }
                }
            }
        }