Beispiel #1
0
        internal static void LocalizeWebParts(this File file, Web web, TokenParser parser, Microsoft.SharePoint.Client.File targetFile, PnPMonitoredScope scope)
        {
            var url      = targetFile.ServerRelativeUrl;
            var webParts = file.WebParts;

            LocalizeParts(web, parser, url, webParts, scope);
        }
        private void AddFileToTemplate(ProvisioningTemplate template, Stream fs, string folder, string fileName, string container)
        {
            var source = !string.IsNullOrEmpty(container) ? (container + "/" + fileName) : fileName;

            template.Connector.SaveFileStream(fileName, container, fs);

            if (template.Connector is ICommitableFileConnector)
            {
                ((ICommitableFileConnector)template.Connector).Commit();
            }

            var existing = template.Files.FirstOrDefault(f =>
                                                         f.Src == $"{container}/{fileName}" &&
                                                         f.Folder == folder);

            if (existing != null)
            {
                template.Files.Remove(existing);
            }

            var newFile = new OfficeDevPnP.Core.Framework.Provisioning.Model.File
            {
                Src       = source,
                Folder    = folder,
                Level     = FileLevel,
                Overwrite = FileOverwrite,
            };

            template.Files.Add(newFile);

            // Determine the output file name and path
            var outFileName = System.IO.Path.GetFileName(Path);
            var outPath     = new FileInfo(Path).DirectoryName;

            var fileSystemConnector = new FileSystemConnector(outPath, "");
            var formatter           = XMLPnPSchemaFormatter.LatestFormatter;
            var extension           = new FileInfo(Path).Extension.ToLowerInvariant();

            if (extension == ".pnp")
            {
                var provider         = new XMLOpenXMLTemplateProvider(template.Connector as OpenXMLConnector);
                var templateFileName = outFileName.Substring(0, outFileName.LastIndexOf(".", StringComparison.Ordinal)) + ".xml";

                provider.SaveAs(template, templateFileName, formatter, TemplateProviderExtensions);
            }
            else
            {
                XMLTemplateProvider provider = new XMLFileSystemTemplateProvider(Path, "");
                provider.SaveAs(template, Path, formatter, TemplateProviderExtensions);
            }
        }
        // private ProvisioningTemplate GetFileContents(Web web, ProvisioningTemplate template, string welcomePageUrl, ProvisioningTemplateCreationInformation creationInfo, PnPMonitoredScope scope)
        //#

        private ProvisioningTemplate GetFileContents(ProvisioningTemplate template, Microsoft.SharePoint.Client.File file, ProvisioningTemplateCreationInformation creationInfo, PnPMonitoredScope scope)
        {
            var listItem   = file.EnsureProperty(p => p.ListItemAllFields);
            var fileUrl    = file.ServerRelativeUrl;
            var folderPath = fileUrl.Substring(0, fileUrl.LastIndexOf("/"));

            var homeFile = new OfficeDevPnP.Core.Framework.Provisioning.Model.File()
            {
                Folder    = folderPath.TokenizeUrl(web.Url),
                Src       = file.ServerRelativeUrl,
                Overwrite = true,
            };

            // Add field values to file
            if (listItem != null && listItem.FieldValues != null)
            {
                homeFile.Properties = listItem.ToProvisioningValues();
            }
            // Add WebParts to file, if it is a page.
            if (System.IO.Path.GetExtension(file.ServerRelativeUrl) == ".aspx")
            {
                var webParts = web.GetWebParts(file.ServerRelativeUrl);

                foreach (var webPart in webParts)
                {
                    var webPartxml = TokenizeWebPartXml(web, web.GetWebPartXml(webPart.Id, file.ServerRelativeUrl));

                    var newWp = new OfficeDevPnP.Core.Framework.Provisioning.Model.WebPart()
                    {
                        Title    = webPart.WebPart.Title,
                        Row      = (uint)webPart.WebPart.ZoneIndex,
                        Order    = (uint)webPart.WebPart.ZoneIndex,
                        Contents = webPartxml
                    };
#if !SP2016
                    // As long as we've no CSOM library that has the ZoneID we can't use the version check as things don't compile...
                    if (web.Context.HasMinimalServerLibraryVersion(Constants.MINIMUMZONEIDREQUIREDSERVERVERSION))
                    {
                        newWp.Zone = webPart.ZoneId;
                    }
#endif
                    homeFile.WebParts.Add(newWp);
                }
            }
            template.Files.Add(homeFile);
            creationInfo.PersistFile(folderPath, file.Name, web, scope);
            return(template);
        }
Beispiel #4
0
        private List <OfficeDevPnP.Core.Framework.Provisioning.Model.File> EnumerateFiles(string folder, string ctid, Hashtable properties)
        {
            var files = new List <OfficeDevPnP.Core.Framework.Provisioning.Model.File>();

            DirectoryInfo dirInfo = new DirectoryInfo(folder);

            foreach (var directory in dirInfo.GetDirectories().Where(d => (d.Attributes & FileAttributes.Hidden) == 0))
            {
                files.AddRange(EnumerateFiles(directory.FullName, ctid, properties));
            }

            var fileInfo = dirInfo.GetFiles(Match);

            foreach (var file in fileInfo.Where(f => (f.Attributes & FileAttributes.Hidden) == 0))
            {
                var unrootedPath = file.FullName.Substring(Folder.Length + 1);
                var targetFolder = Path.Combine(TargetFolder, unrootedPath.LastIndexOf("\\") > -1 ? unrootedPath.Substring(0, unrootedPath.LastIndexOf("\\")) : "");
                targetFolder = targetFolder.Replace('\\', '/');
                var modelFile = new OfficeDevPnP.Core.Framework.Provisioning.Model.File()
                {
                    Folder    = targetFolder,
                    Overwrite = true,
                    Src       = unrootedPath,
                };
                if (ctid != null)
                {
                    modelFile.Properties.Add("ContentTypeId", ctid);
                }
                if (properties != null && properties.Count > 0)
                {
                    foreach (var key in properties.Keys)
                    {
                        modelFile.Properties.Add(key.ToString(), properties[key].ToString());
                    }
                }
                modelFile.Security = null;
                files.Add(modelFile);
            }

            return(files);
        }
        private List<OfficeDevPnP.Core.Framework.Provisioning.Model.File> EnumerateFiles(string folder, string ctid, Hashtable properties)
        {
            var files = new List<OfficeDevPnP.Core.Framework.Provisioning.Model.File>();

            DirectoryInfo dirInfo = new DirectoryInfo(folder);

            foreach (var directory in dirInfo.GetDirectories().Where(d => (d.Attributes & FileAttributes.Hidden) == 0))
            {
                files.AddRange(EnumerateFiles(directory.FullName, ctid, properties));
            }

            var fileInfo = dirInfo.GetFiles(Match);
            foreach (var file in fileInfo.Where(f => (f.Attributes & FileAttributes.Hidden) == 0))
            {
                var unrootedPath = file.FullName.Substring(Folder.Length + 1);
                var targetFolder = Path.Combine(TargetFolder, unrootedPath.LastIndexOf("\\") > -1 ? unrootedPath.Substring(0, unrootedPath.LastIndexOf("\\")) : "");
                targetFolder = targetFolder.Replace('\\', '/');
                var modelFile = new OfficeDevPnP.Core.Framework.Provisioning.Model.File()
                {
                    Folder = targetFolder,
                    Overwrite = true,
                    Src = unrootedPath,
                };
                if (ctid != null)
                {
                    modelFile.Properties.Add("ContentTypeId", ctid);
                }
                if (properties != null && properties.Count > 0)
                {
                    foreach (var key in properties.Keys)
                    {
                        modelFile.Properties.Add(key.ToString(), properties[key].ToString());
                    }
                }
                modelFile.Security = null;
                files.Add(modelFile);
            }

            return files;
        }
Beispiel #6
0
        private OfficeDevPnP.Core.Framework.Provisioning.Model.File RetrieveFieldValues(Web web,
                                                                                        Microsoft.SharePoint.Client.File file, OfficeDevPnP.Core.Framework.Provisioning.Model.File modelFile)
        {
            try
            {
                Microsoft.SharePoint.Client.ListItem listItem = null;
                try
                {
                    listItem = file.EnsureProperty(f => f.ListItemAllFields);
                }
                catch
                {
                }

                if (listItem != null)
                {
                    var list = listItem.ParentList;

                    var fields = list.Fields;
                    web.Context.Load(fields,
                                     fs => fs.IncludeWithDefaultProperties(f => f.TypeAsString, f => f.InternalName, f => f.Title));
                    web.Context.ExecuteQueryRetry();

                    var fieldValues = listItem.FieldValues;

                    var fieldValuesAsText = listItem.EnsureProperty(li => li.FieldValuesAsText).FieldValues;

                    var fieldstoExclude = new[]
                    {
                        "ID",
                        "GUID",
                        "Author",
                        "Editor",
                        "FileLeafRef",
                        "FileRef",
                        "File_x0020_Type",
                        "Modified_x0020_By",
                        "Created_x0020_By",
                        "Created",
                        "Modified",
                        "FileDirRef",
                        "Last_x0020_Modified",
                        "Created_x0020_Date",
                        "File_x0020_Size",
                        "FSObjType",
                        "IsCheckedoutToLocal",
                        "ScopeId",
                        "UniqueId",
                        "VirusStatus",
                        "_Level",
                        "_IsCurrentVersion",
                        "ItemChildCount",
                        "FolderChildCount",
                        "SMLastModifiedDate",
                        "owshiddenversion",
                        "_UIVersion",
                        "_UIVersionString",
                        "Order",
                        "WorkflowVersion",
                        "DocConcurrencyNumber",
                        "ParentUniqueId",
                        "CheckedOutUserId",
                        "SyncClientId",
                        "CheckedOutTitle",
                        "SMTotalSize",
                        "SMTotalFileStreamSize",
                        "SMTotalFileCount",
                        "ParentVersionString",
                        "ParentLeafName",
                        "SortBehavior",
                        "StreamHash",
                        "TaxCatchAll",
                        "TaxCatchAllLabel",
                        "_ModerationStatus",
                        //"HtmlDesignAssociated",
                        //"HtmlDesignStatusAndPreview",
                        "MetaInfo",
                        "CheckoutUser",
                        "NoExecute",
                        "_HasCopyDestinations",
                        "ContentVersion",
                        "UIVersion",
                    };

                    foreach (var fieldValue in fieldValues.Where(f => !fieldstoExclude.Contains(f.Key)))
                    {
                        if (fieldValue.Value != null && !string.IsNullOrEmpty(fieldValue.Value.ToString()))
                        {
                            var field = fields.FirstOrDefault(fs => fs.InternalName == fieldValue.Key);

                            string value = string.Empty;

                            switch (field.TypeAsString)
                            {
                            case "URL":
                                value = Tokenize(fieldValuesAsText[fieldValue.Key], web.Url, web);
                                break;

                            case "User":
                                var userFieldValue = fieldValue.Value as Microsoft.SharePoint.Client.FieldUserValue;
                                if (userFieldValue != null)
                                {
#if !ONPREMISES
                                    value = userFieldValue.Email;
#else
                                    value = userFieldValue.LookupValue;
#endif
                                }
                                break;

                            case "LookupMulti":
                                var lookupFieldValue =
                                    fieldValue.Value as Microsoft.SharePoint.Client.FieldLookupValue[];
                                if (lookupFieldValue != null)
                                {
                                    value = Tokenize(JsonUtility.Serialize(lookupFieldValue), web.Url);
                                }
                                break;

                            case "TaxonomyFieldType":
                                var taxonomyFieldValue =
                                    fieldValue.Value as Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValue;
                                if (taxonomyFieldValue != null)
                                {
                                    value = Tokenize(JsonUtility.Serialize(taxonomyFieldValue), web.Url);
                                }
                                break;

                            case "TaxonomyFieldTypeMulti":
                                var taxonomyMultiFieldValue =
                                    fieldValue.Value as
                                    Microsoft.SharePoint.Client.Taxonomy.TaxonomyFieldValueCollection;
                                if (taxonomyMultiFieldValue != null)
                                {
                                    value = Tokenize(JsonUtility.Serialize(taxonomyMultiFieldValue), web.Url);
                                }
                                break;

                            case "ContentTypeIdFieldType":
                            default:
                                value = Tokenize(fieldValue.Value.ToString(), web.Url, web);
                                break;
                            }

                            if (fieldValue.Key == "ContentTypeId")
                            {
                                // Replace the content typeid with a token
                                var ct = list.GetContentTypeById(value);
                                if (ct != null)
                                {
                                    value = string.Format("{{contenttypeid:{0}}}", ct.Name);
                                }
                            }

                            // We process real values only
                            if (value != null && !String.IsNullOrEmpty(value) && value != "[]")
                            {
                                modelFile.Properties.Add(fieldValue.Key, value);
                            }
                        }
                    }
                }

                return(modelFile);
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
Beispiel #7
0
        private ProvisioningTemplate GetFileContents(Web web, ProvisioningTemplate template, string pageFullUrl, ProvisioningTemplateCreationInformation creationInfo, string pageUrl)
        {
            try
            {
                var fullUri = new Uri(UrlUtility.Combine(web.Url, pageUrl));

                var folderPath =
                    fullUri.Segments.Take(fullUri.Segments.Count() - 1)
                    .ToArray()
                    .Aggregate((i, x) => i + x)
                    .TrimEnd('/');
                var fileName = fullUri.Segments[fullUri.Segments.Count() - 1];

                var webParts = web.GetWebParts(pageFullUrl);

                var file = web.GetFileByServerRelativeUrl(pageFullUrl);

                var homeFile = new OfficeDevPnP.Core.Framework.Provisioning.Model.File()
                {
                    Folder    = Tokenize(folderPath, web.Url),
                    Src       = fileName,
                    Overwrite = true,
                };

                // Add field values to file

                RetrieveFieldValues(web, file, homeFile);

                // Add WebParts to file
                foreach (var webPart in webParts)
                {
                    var webPartxml = TokenizeWebPartXml(web, web.GetWebPartXml(webPart.Id, pageFullUrl));

                    OfficeDevPnP.Core.Framework.Provisioning.Model.WebPart newWp = new OfficeDevPnP.Core.Framework.
                                                                                   Provisioning.Model.WebPart()
                    {
                        Title    = webPart.WebPart.Title,
                        Row      = (uint)webPart.WebPart.ZoneIndex,
                        Order    = (uint)webPart.WebPart.ZoneIndex,
                        Contents = webPartxml
                    };
#if !SP2016
                    // As long as we've no CSOM library that has the ZoneID we can't use the version check as things don't compile...
                    if (web.Context.HasMinimalServerLibraryVersion(Constants.MINIMUMZONEIDREQUIREDSERVERVERSION))
                    {
                        newWp.Zone = webPart.ZoneId;
                    }
#endif
                    homeFile.WebParts.Add(newWp);
                }
                template.Files.Add(homeFile);

                // Persist file using connector
                if (creationInfo.PersistBrandingFiles)
                {
                    PersistFile(web, creationInfo, folderPath, fileName);
                }
                return(template);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }