Exemplo n.º 1
0
        public override ProvisioningTemplate ExtractObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateCreationInformation creationInfo)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                var lists = this.GetListsWithPages(template);
                template.Pages = new PageCollection(template);

                var homePageUrl = web.GetHomePageRelativeUrl();
                foreach (var list in lists)
                {
                    try
                    {
                        List splist = web.Lists.GetById(list.ID);
                        web.Context.Load(splist);
                        web.Context.ExecuteQueryRetry();
                        if (!creationInfo.ExecutePreProvisionEvent<ListInstance, List>(Handlers.Pages, template, list, null))
                        {
                            continue;
                        }

                        var listItems = GetListPages(web, splist);
                        var fileItems = listItems.AsEnumerable().Where(x => x.IsFile());
                        foreach (ListItem item in fileItems)
                        {
                            try
                            {
                                IPageModelProvider provider = GetProvider(item, homePageUrl, web, parser);
                                if (null != provider)
                                {
                                    provider.AddPage(item, template);
                                }
                            }
                            catch (Exception ex)
                            {
                                var message = string.Format("Error in export page for list: {0}", list.ServerRelativeUrl);
                                scope.LogError(ex, message);
                            }
                        }

                        creationInfo.ExecutePostProvisionEvent<ListInstance, List>(Handlers.Pages, template, list, splist);
                    }
                    catch (Exception exception)
                    {
                        var message = string.Format("Error in export publishing page for list: {0}", list.ServerRelativeUrl);
                        scope.LogError(exception, message);
                    }
                }
                // Impossible to return all files in the site currently

                // If a base template is specified then use that one to "cleanup" the generated template model
                if (creationInfo.BaseTemplate != null)
                {
                    template = CleanupEntities(template, creationInfo.BaseTemplate);
                }
            }
            return template;
        }
        public override ProvisioningTemplate ExtractObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateCreationInformation creationInfo)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                foreach (var listInstance in template.Lists)
                {
                    List list = web.Lists.GetById(listInstance.ID);
                    web.Context.Load(list);
                    web.Context.ExecuteQueryRetry();

                    if (creationInfo.ExecutePreProvisionEvent<ListInstance, List>(Handlers.ListContents, template, listInstance, list))
                    {
                        ListItemsProvider provider = new ListItemsProvider(list, web, template);
                        List<DataRow> dataRows = provider.ExtractItems(creationInfo, parser, scope);
                        listInstance.DataRows.AddRange(dataRows);

                        creationInfo.ExecutePostProvisionEvent<ListInstance, List>(Handlers.ListContents, template, listInstance, list);
                    }
                }
            }
            return template;
        }
Exemplo n.º 3
0
        public override ProvisioningTemplate ExtractObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateCreationInformation creationInfo)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                // if this is a sub site then we're not creating field entities.
                if (web.IsSubSite())
                {
                    scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_Fields_Context_web_is_subweb__skipping_site_columns);
                    return template;
                }

                var existingFields = web.Fields;
                web.Context.Load(web, w => w.ServerRelativeUrl);
                web.Context.Load(existingFields);
                web.Context.Load(web.Lists, ls => ls.Include(l => l.Id, l => l.Title));
                web.Context.ExecuteQueryRetry();

                var taxTextFieldsToMoveUp = new List<Guid>();

                foreach (var field in existingFields)
                {
                    if (!BuiltInFieldId.Contains(field.Id))
                    {
                        if (creationInfo.ExecutePreProvisionEvent<Field,SPField>(Handlers.Fields, template, null, field))
                        {
                            var fieldXml = field.SchemaXml;
                            XElement element = XElement.Parse(fieldXml);

                            // Check if the field contains a reference to a list. If by Guid, rewrite the value of the attribute to use web relative paths
                            var listIdentifier = element.Attribute("List") != null ? element.Attribute("List").Value : null;
                            if (!string.IsNullOrEmpty(listIdentifier))
                            {
                                var listGuid = Guid.Empty;
                                fieldXml = ParseFieldSchema(fieldXml, web.Lists);
                                element = XElement.Parse(fieldXml);
                                //if (Guid.TryParse(listIdentifier, out listGuid))
                                //{
                                //    fieldXml = ParseListSchema(fieldXml, web.Lists);
                                //if (newfieldXml == fieldXml)
                                //{
                                //    var list = web.Lists.GetById(listGuid);
                                //    web.Context.Load(list, l => l.RootFolder.ServerRelativeUrl);
                                //    web.Context.ExecuteQueryRetry();

                                //    var listUrl = list.RootFolder.ServerRelativeUrl.Substring(web.ServerRelativeUrl.Length).TrimStart('/');
                                //    element.Attribute("List").SetValue(listUrl);
                                //    fieldXml = element.ToString();
                                //}
                                //}
                            }
                            // Check if the field is of type TaxonomyField
                            if (field.TypeAsString.StartsWith("TaxonomyField"))
                            {
                                var taxField = (TaxonomyField)field;
                                web.Context.Load(taxField, tf => tf.TextField, tf => tf.Id);
                                web.Context.ExecuteQueryRetry();
                                taxTextFieldsToMoveUp.Add(taxField.TextField);
                            }
                            // Check if we have version attribute. Remove if exists
                            if (element.Attribute("Version") != null)
                            {
                                element.Attributes("Version").Remove();
                                fieldXml = element.ToString();
                            }
                            Field templateField = new Field() { SchemaXml = fieldXml };
                            template.SiteFields.Add(templateField);

                            creationInfo.ExecutePostProvisionEvent<Field, SPField>(Handlers.Fields, template, templateField, field);
                        }
                    }
                }
                // move hidden taxonomy text fields to the top of the list
                foreach (var textFieldId in taxTextFieldsToMoveUp)
                {
                    var field = template.SiteFields.First(f => Guid.Parse(f.SchemaXml.ElementAttributeValue("ID")).Equals(textFieldId));
                    template.SiteFields.RemoveAll(f => Guid.Parse(f.SchemaXml.ElementAttributeValue("ID")).Equals(textFieldId));
                    template.SiteFields.Insert(0, field);
                }
                // If a base template is specified then use that one to "cleanup" the generated template model
                if (creationInfo.BaseTemplate != null)
                {
                    template = CleanupEntities(template, creationInfo.BaseTemplate);
                }
            }
            return template;
        }
Exemplo n.º 4
0
        public override ProvisioningTemplate ExtractObjects(Web web, ProvisioningTemplate template, TokenParser parser, ProvisioningTemplateCreationInformation creationInfo)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                web.EnsureProperties(w => w.ServerRelativeUrl, w => w.Url, w => w.Id);

                var serverRelativeUrl = web.ServerRelativeUrl;

                // For each list in the site
                var lists = web.Lists;

                web.Context.Load(lists,
                    lc => lc.IncludeWithDefaultProperties(
                        l => l.ContentTypes.IncludeWithDefaultProperties(
                            ct => ct.Parent,
                            ct => ct.Parent.StringId,
                            ct => ct.FieldLinks
                            ),
                        l => l.Id,
                        l => l.ParentWebUrl,
                        l => l.Views,
                        l => l.DefaultNewFormUrl,
                        l => l.DefaultDisplayFormUrl,
                        l => l.DefaultEditFormUrl,
                        l => l.BaseTemplate,
                        l => l.OnQuickLaunch,
                        l => l.RootFolder.ServerRelativeUrl,
                        l => l.Fields.IncludeWithDefaultProperties(
                            f => f.Id,
                            f => f.Title,
                            f => f.Hidden,
                            f => f.InternalName,
                            f => f.Required),
                        l => l.HasUniqueRoleAssignments));

                web.Context.ExecuteQueryRetry();

                // Let's see if there are workflow subscriptions
                Microsoft.SharePoint.Client.WorkflowServices.WorkflowSubscription[] workflowSubscriptions = null;
                try
                {
                    workflowSubscriptions = web.GetWorkflowSubscriptions();
                }
                catch (ServerException)
                {
                    // If there is no workflow service present in the farm this method will throw an error.
                    // Swallow the exception
                }

                // Retrieve all not hidden lists and the Workflow History Lists, just in case there are active workflow subscriptions
                var includeWorkflowSubscriptions = workflowSubscriptions != null && workflowSubscriptions.Length > 0;
                var allowedLists = lists.AsEnumerable().Where(l => !l.Hidden || includeWorkflowSubscriptions && l.BaseTemplate == 140);

                //Performance improvements: retrieve site columns for extract list fields
                var siteColumns = web.AvailableFields;
                web.Context.Load(siteColumns, scs => scs.Include(sc => sc.Id));
                web.Context.ExecuteQueryRetry();

                foreach (var siteList in allowedLists)
                {
                    ListInstance baseTemplateList = null;
                    if (creationInfo.BaseTemplate != null)
                    {
                        // Check if we need to skip this list...if so let's do it before we gather all the other information for this list...improves performance
                        var index = creationInfo.BaseTemplate.Lists.FindIndex(f => f.Url.Equals(siteList.RootFolder.ServerRelativeUrl.Substring(serverRelativeUrl.Length + 1)) &&
                                                                                   f.TemplateType.Equals(siteList.BaseTemplate));
                        if (index != -1)
                        {
                            baseTemplateList = creationInfo.BaseTemplate.Lists[index];
                            if (siteList.Hidden && !(includeWorkflowSubscriptions && siteList.BaseTemplate == 140))
                            {
                                continue;
                            }
                        }
                    }

                    if (!creationInfo.ExecutePreProvisionEvent<ListInstance, List>(Handlers.Lists, template, null, siteList))
                    {
                        continue;
                    }

                    string documentTemplateContent = string.Empty;
                    if (!String.IsNullOrEmpty(siteList.DocumentTemplateUrl)) {
                        documentTemplateContent = web.GetFileAsString(siteList.DocumentTemplateUrl);
                    }

                    var contentTypeFields = new List<FieldRef>();
                    var list = new ListInstance
                    {
                        Description = siteList.Description,
                        EnableVersioning = siteList.EnableVersioning,
                        TemplateType = siteList.BaseTemplate,
                        Title = siteList.Title,
                        Hidden = siteList.Hidden,
                        EnableFolderCreation = siteList.EnableFolderCreation,
                        DocumentTemplate = TokenizeUrl(siteList.DocumentTemplateUrl, parser),
                        DocumentTemplateContent = documentTemplateContent,
                        ContentTypesEnabled = siteList.ContentTypesEnabled,
                        Url = siteList.RootFolder.ServerRelativeUrl.Substring(serverRelativeUrl.Length).TrimStart('/'),
                        TemplateFeatureID = siteList.TemplateFeatureId,
                        EnableAttachments = siteList.EnableAttachments,
                        OnQuickLaunch = siteList.OnQuickLaunch,
                        MaxVersionLimit =
                            siteList.IsObjectPropertyInstantiated("MajorVersionLimit") ? siteList.MajorVersionLimit : 0,
                        EnableMinorVersions = siteList.EnableMinorVersions,
                        MinorVersionLimit =
                            siteList.IsObjectPropertyInstantiated("MajorWithMinorVersionsLimit")
                                ? siteList.MajorWithMinorVersionsLimit
                                : 0,
                        ParentWebUrl = web.Url,
                        ID = siteList.Id,
                        ParentWebId = web.Id,
                        ServerRelativeUrl = siteList.RootFolder.ServerRelativeUrl
                    };

                    try
                    {
                        list = ExtractForms(web, siteList, list);
                    }
                    catch (Exception ex)
                    {
                        scope.LogDebug("Extract forms on list {0} failed. {1}. {2}", list.Title, ex.Message, ex.StackTrace);
                    }

                    list = ExtractContentTypes(web, siteList, contentTypeFields, list);

                    list = ExtractViews(siteList, list);

                    list = ExtractFields(web, siteList, contentTypeFields, list, lists, siteColumns);

                    if (siteList.HasUniqueRoleAssignments)
                    {
                        list.Security = siteList.GetSecurity(parser);
                    }

                    var logCTWarning = false;
                    if (baseTemplateList != null)
                    {
                        if (!baseTemplateList.Equals(list))
                        {
                            scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstances_Adding_list___0_____1_, list.Title, list.Url);
                            template.Lists.Add(list);
                            if (list.ContentTypesEnabled && list.ContentTypeBindings.Any() && web.IsSubSite())
                            {
                                logCTWarning = true;
                            }
                        }
                    }
                    else
                    {
                        scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ListInstances_Adding_list___0_____1_, list.Title, list.Url);
                        template.Lists.Add(list);
                        if (list.ContentTypesEnabled && list.ContentTypeBindings.Any() && web.IsSubSite())
                        {
                            logCTWarning = true;
                        }

                    }

                    creationInfo.ExecutePostProvisionEvent<ListInstance, List>(Handlers.Lists, template, list, siteList);

                    if (logCTWarning)
                    {
                        scope.LogWarning("You are extracting a template from a subweb. List '{0}' refers to content types. Content types are not exported when extracting a template from a subweb", list.Title);
                        WriteWarning(string.Format("You are extracting a template from a subweb. List '{0}' refers to content types. Content types are not exported when extracting a template from a subweb", list.Title), ProvisioningMessageType.Warning);
                    }
                }

            }
            return template;
        }