//protected void WithExistingWebPart(ListItem listItem, WebPartDefinition webPartModel,
        //  Action<WebPart> action)
        //{
        //    WithExistingWebPart(listItem.File, webPartModel, action);
        //}

        protected void WithExistingWebPart(File file, WebPartDefinition webPartModel,
           Action<WebPart> action)
        {
            WithExistingWebPart(file, webPartModel, (w, d) =>
            {
                action(w);
            });
        }
        public DocumentStream(ClientContext ctx, ListItem documentItem)
        {
            _ctx = ctx;

            _documentFile = documentItem.File;
            ctx.Load(_documentFile);
            ctx.Load(_documentFile.Versions);
            ctx.ExecuteQuery();

            MajorVersionLabel = _documentFile.MajorVersion + ".0";
            FileName = _documentFile.Name;
        }
        public SPOContentReaderWriter(File file, bool isBinary)
        {
            _file = file;
            _isBinary = isBinary;
            _stream = new MemoryStream();

            var spStream = _file.OpenBinaryStream();
            _file.Context.ExecuteQuery();
            spStream.Value.CopyTo(_stream);
            _stream.Position = 0;

            _streamWriter = new StreamWriter(_stream);
            _streamReader = new StreamReader(_stream);
        }
        public void AddWebParts(ClientContext context, File uploadFile, List<ShWebPartReference> webPartReferences, bool replaceWebParts)
        {
            // we should be allowed to delete webparts (by using replaceWebparts and define no new ones
            if (webPartReferences.Count <= 0 && !replaceWebParts) return;

            var limitedWebPartManager = uploadFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

            context.Load(limitedWebPartManager, manager => manager.WebParts);
            context.ExecuteQuery();

            if (limitedWebPartManager.WebParts.Count == 0 || replaceWebParts)
            {
                for (int i = limitedWebPartManager.WebParts.Count - 1; i >= 0; i--)
                {
                    limitedWebPartManager.WebParts[i].DeleteWebPart();
                }
                context.ExecuteQuery();

                foreach (ShWebPartReference webPart in webPartReferences)
                {
                    //Convention: All webparts are located in the content/webparts folder
                    var webPartPath = Path.Combine(_contentDirectoryPath, "webparts", webPart.FileName);
                    var webPartFileContent = System.IO.File.ReadAllText(webPartPath);
                    if (!System.IO.File.Exists(webPartPath))
                    {
                        Log.ErrorFormat("Webpart at path {0} not found", webPartPath);
                        continue;
                    }

                    //Token replacement in the webpart XML
                    webPartFileContent = GetPropertyValueWithTokensReplaced(webPartFileContent, context);
                    var webPartDefinition = limitedWebPartManager.ImportWebPart(webPartFileContent);
                    if (webPart.PropertiesOverrides.Count > 0)
                    {
                        foreach (KeyValuePair<string, string> propertyOverride in webPart.PropertiesOverrides)
                        {
                            //Token replacement in the PropertiesOverrides JSON array
                            var propOverrideValue = GetPropertyValueWithTokensReplaced(propertyOverride.Value, context);
                            webPartDefinition.WebPart.Properties[propertyOverride.Key] = propOverrideValue;
                        }
                    }
                    limitedWebPartManager.AddWebPart(webPartDefinition.WebPart, webPart.ZoneID, webPart.Order);
                }

                context.Load(limitedWebPartManager);
                context.ExecuteQuery();
            }
        }
Beispiel #5
0
 private WebPartDefinition AddWebPart(Web web, WebPart webPart, File pageFile)
 {
     LimitedWebPartManager limitedWebPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
     WebPartDefinition oWebPartDefinition = limitedWebPartManager.ImportWebPart(webPart.Contents);
     WebPartDefinition wpdNew = limitedWebPartManager.AddWebPart(oWebPartDefinition.WebPart, webPart.Zone, (int)webPart.Order);
     web.Context.Load(wpdNew, x => x.Id);
     web.Context.ExecuteQueryRetry();
     return wpdNew;
 }
Beispiel #6
0
 public void SetFileProperties(File file, IDictionary <string, string> properties, bool checkoutIfRequired = true)
 {
     SetFileProperties(file, properties, null, checkoutIfRequired);
 }
Beispiel #7
0
        public string UploadSPFile(SharePointContext spContext, HttpPostedFileBase tempFile, string documentFolder, string siteUrl, string prefix)
        {
            log.Info("- OutboxDocs - UploadSPFile");
            var result = string.Empty;

            using (var clientContext = spContext.CreateAppOnlyClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    try
                    {
                        var relativeSite = clientContext.Url.Substring(clientContext.Url.IndexOf('/', 10));
                        var folder       = clientContext.Web.GetFolderByServerRelativeUrl(documentFolder);
                        clientContext.Load(folder);
                        clientContext.ExecuteQuery();
                        FileCreationInformation fci = new FileCreationInformation();
                        if (tempFile.ContentLength < 1000000) // ~1MB
                        {
                            using (System.IO.Stream inputStream = tempFile.InputStream)
                            {
                                var memoryStream = inputStream as System.IO.MemoryStream;
                                if (memoryStream == null)
                                {
                                    memoryStream = new System.IO.MemoryStream();
                                    inputStream.CopyTo(memoryStream);
                                }
                                fci.Content = memoryStream.ToArray();
                            }
                            fci.Url       = documentFolder + "/" + prefix + tempFile.FileName;
                            fci.Overwrite = true;
                            Microsoft.SharePoint.Client.File fileToUpload = folder.Files.Add(fci);
                            clientContext.Load(fileToUpload);
                            clientContext.ExecuteQuery();
                            Uri hostUrl = new Uri(siteUrl);
                            result = "https://" + hostUrl.Host + fileToUpload.ServerRelativeUrl;
                        }
                        else if (tempFile.ContentLength < 5000000) // ~5MB
                        {
                            fci.ContentStream = tempFile.InputStream;
                            fci.Url           = documentFolder + "/" + prefix + tempFile.FileName;
                            fci.Overwrite     = true;
                            Microsoft.SharePoint.Client.File fileToUpload = folder.Files.Add(fci);
                            clientContext.Load(fileToUpload);
                            clientContext.ExecuteQuery();
                            Uri hostUrl = new Uri(siteUrl);
                            result = "https://" + hostUrl.Host + fileToUpload.ServerRelativeUrl;
                        }
                        if (tempFile.ContentLength >= 5000000) // > ~5MB
                        {
                            Guid uploadId = Guid.NewGuid();
                            Microsoft.SharePoint.Client.File uploadFile;
                            int blockSize = 4 * 1024 * 1024; // 5MB blocks
                            ClientResult <long> bytesUploaded = null;
                            byte[] buffer         = new byte[blockSize];
                            Byte[] lastBuffer     = null;
                            long   fileoffset     = 0;
                            long   totalBytesRead = 0;
                            int    bytesRead;
                            bool   first = true;
                            bool   last  = false;
                            using (BinaryReader br = new BinaryReader(tempFile.InputStream))
                            {
                                while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    totalBytesRead = totalBytesRead + bytesRead;

                                    // You've reached the end of the file.
                                    if (totalBytesRead == tempFile.ContentLength)
                                    {
                                        last = true;
                                        // Copy to a new buffer that has the correct size.
                                        lastBuffer = new byte[bytesRead];
                                        Array.Copy(buffer, 0, lastBuffer, 0, bytesRead);
                                    }

                                    if (first)
                                    {
                                        using (MemoryStream contentStream = new MemoryStream())
                                        {
                                            // Add an empty file.
                                            FileCreationInformation fileInfo = new FileCreationInformation();
                                            fileInfo.ContentStream = contentStream;
                                            fileInfo.Url           = prefix + tempFile.FileName;
                                            fileInfo.Overwrite     = true;
                                            uploadFile             = folder.Files.Add(fileInfo);

                                            // Start upload by uploading the first slice.
                                            using (MemoryStream s = new MemoryStream(buffer))
                                            {
                                                // Call the start upload method on the first slice.
                                                bytesUploaded = uploadFile.StartUpload(uploadId, s);
                                                clientContext.ExecuteQuery();
                                                // fileoffset is the pointer where the next slice will be added.
                                                fileoffset = bytesUploaded.Value;
                                            }

                                            // You can only start the upload once.
                                            first = false;
                                        }
                                    }
                                    else
                                    {
                                        // Get a reference to your file.
                                        uploadFile = clientContext.Web.GetFileByServerRelativeUrl(folder.ServerRelativeUrl + System.IO.Path.AltDirectorySeparatorChar + prefix + tempFile.FileName);

                                        if (last)
                                        {
                                            // Is this the last slice of data?
                                            using (MemoryStream s = new MemoryStream(lastBuffer))
                                            {
                                                // End sliced upload by calling FinishUpload.
                                                uploadFile = uploadFile.FinishUpload(uploadId, fileoffset, s);
                                                clientContext.Load(uploadFile);
                                                clientContext.ExecuteQuery();

                                                // Return the file object for the uploaded file.
                                                Uri hostUrl = new Uri(siteUrl);
                                                result = "https://" + hostUrl.Host + uploadFile.ServerRelativeUrl;
                                            }
                                        }
                                        else
                                        {
                                            using (MemoryStream s = new MemoryStream(buffer))
                                            {
                                                // Continue sliced upload.
                                                bytesUploaded = uploadFile.ContinueUpload(uploadId, fileoffset, s);
                                                clientContext.ExecuteQuery();
                                                // Update fileoffset for the next slice.
                                                fileoffset = bytesUploaded.Value;
                                            }
                                        }
                                    }
                                } // while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.ToString());
                    }
                }
            }
            return(result);
        }
Beispiel #8
0
        /// <summary>
        /// Copy the file from the source to the target location
        /// </summary>
        /// <param name="sourceFileUrl"></param>
        /// <param name="targetLocationUrl"></param>
        /// <remarks>
        ///     Based on the documentation: https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/upload-large-files-sample-app-for-sharepoint
        /// </remarks>
        public string CopyAssetToTargetLocation(string sourceFileUrl, string targetLocationUrl, int fileChunkSizeInMB = 3)
        {
            // This copies the latest version of the asset to the target site collection
            // Going to need to add a bunch of checks to ensure the target file exists

            // Each sliced upload requires a unique ID.
            Guid uploadId = Guid.NewGuid();
            // Calculate block size in bytes.
            int  blockSize     = fileChunkSizeInMB * 1024 * 1024;
            bool fileOverwrite = true;

            Stream sourceStream    = null;
            var    sourceAssetFile = _sourceClientContext.Web.GetFileByServerRelativeUrl(sourceFileUrl);

            _sourceClientContext.Load(sourceAssetFile, s => s.Exists);
            _sourceClientContext.ExecuteQueryRetry();

            if (sourceAssetFile.Exists)
            {
                // Test ByPass
                //if 2010 then

                if (_sourceContextSPVersion == SPVersion.SP2010)
                {
                    sourceStream = new MemoryStream();

                    if (_sourceClientContext.HasPendingRequest)
                    {
                        _sourceClientContext.ExecuteQueryRetry();
                    }
                    var fileBinary = File.OpenBinaryDirect(_sourceClientContext, sourceFileUrl);
                    _sourceClientContext.ExecuteQueryRetry();
                    Stream tempSourceStream = fileBinary.Stream;

                    CopyStream(tempSourceStream, sourceStream);

                    //Fix: https://stackoverflow.com/questions/47510815/sharepoint-uploadfile-specified-argument-was-out-of-range-of-valid-values
                    sourceStream.Seek(0, SeekOrigin.Begin);
                }
                else
                {
                    // Get the file from SharePoint

                    ClientResult <System.IO.Stream> sourceAssetFileData = sourceAssetFile.OpenBinaryStream();
                    _sourceClientContext.Load(sourceAssetFile);
                    _sourceClientContext.ExecuteQueryRetry();
                    sourceStream = sourceAssetFileData.Value;
                }

                using (Stream sourceFileStream = sourceStream)
                {
                    string fileName = sourceAssetFile.EnsureProperty(p => p.Name);

                    LogInfo(string.Format(LogStrings.AssetTransferUploading, fileName), LogStrings.Heading_AssetTransfer);

                    // New File object.
                    Microsoft.SharePoint.Client.File uploadFile;

                    // Get the information about the folder that will hold the file.
                    // Add the file to the target site
                    Folder targetFolder = _targetClientContext.Web.GetFolderByServerRelativeUrl(targetLocationUrl);
                    _targetClientContext.Load(targetFolder);
                    _targetClientContext.ExecuteQueryRetry();

                    // Get the file size
                    long fileSize = sourceFileStream.Length;

                    // Process with two approaches
                    if (fileSize <= blockSize)
                    {
                        // Use regular approach.

                        FileCreationInformation fileInfo = new FileCreationInformation();
                        fileInfo.ContentStream = sourceFileStream;
                        fileInfo.Url           = fileName;
                        fileInfo.Overwrite     = fileOverwrite;

                        uploadFile = targetFolder.Files.Add(fileInfo);
                        _targetClientContext.Load(uploadFile);
                        _targetClientContext.ExecuteQueryRetry();

                        LogInfo(string.Format(LogStrings.AssetTransferUploadComplete, fileName), LogStrings.Heading_AssetTransfer);
                        // Return the file object for the uploaded file.
                        return(uploadFile.EnsureProperty(o => o.ServerRelativeUrl));
                    }
                    else
                    {
                        // Use large file upload approach.
                        ClientResult <long> bytesUploaded = null;

                        using (BinaryReader br = new BinaryReader(sourceFileStream))
                        {
                            byte[] buffer         = new byte[blockSize];
                            Byte[] lastBuffer     = null;
                            long   fileoffset     = 0;
                            long   totalBytesRead = 0;
                            int    bytesRead;
                            bool   first = true;
                            bool   last  = false;

                            // Read data from file system in blocks.
                            while ((bytesRead = br.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                totalBytesRead = totalBytesRead + bytesRead;

                                // You've reached the end of the file.
                                if (totalBytesRead == fileSize)
                                {
                                    last = true;
                                    // Copy to a new buffer that has the correct size.
                                    lastBuffer = new byte[bytesRead];
                                    Array.Copy(buffer, 0, lastBuffer, 0, bytesRead);
                                }

                                if (first)
                                {
                                    using (MemoryStream contentStream = new MemoryStream())
                                    {
                                        // Add an empty file.
                                        FileCreationInformation fileInfo = new FileCreationInformation();
                                        fileInfo.ContentStream = contentStream;
                                        fileInfo.Url           = fileName;
                                        fileInfo.Overwrite     = fileOverwrite;
                                        uploadFile             = targetFolder.Files.Add(fileInfo);

                                        // Start upload by uploading the first slice.
                                        using (MemoryStream s = new MemoryStream(buffer))
                                        {
                                            // Call the start upload method on the first slice.
                                            bytesUploaded = uploadFile.StartUpload(uploadId, s);
                                            _targetClientContext.ExecuteQueryRetry();
                                            // fileoffset is the pointer where the next slice will be added.
                                            fileoffset = bytesUploaded.Value;
                                        }

                                        // You can only start the upload once.
                                        first = false;
                                    }
                                }
                                else
                                {
                                    // Get a reference to your file.
                                    var fileUrl = targetFolder.ServerRelativeUrl + System.IO.Path.AltDirectorySeparatorChar + fileName;
                                    uploadFile = _targetClientContext.Web.GetFileByServerRelativeUrl(fileUrl);

                                    if (last)
                                    {
                                        // Is this the last slice of data?
                                        using (MemoryStream s = new MemoryStream(lastBuffer))
                                        {
                                            // End sliced upload by calling FinishUpload.
                                            uploadFile = uploadFile.FinishUpload(uploadId, fileoffset, s);
                                            _targetClientContext.ExecuteQueryRetry();

                                            LogInfo(string.Format(LogStrings.AssetTransferUploadComplete, fileName), LogStrings.Heading_AssetTransfer);
                                            // Return the file object for the uploaded file.
                                            return(fileUrl);
                                        }
                                    }
                                    else
                                    {
                                        using (MemoryStream s = new MemoryStream(buffer))
                                        {
                                            // Continue sliced upload.
                                            bytesUploaded = uploadFile.ContinueUpload(uploadId, fileoffset, s);
                                            _targetClientContext.ExecuteQueryRetry();
                                            // Update fileoffset for the next slice.
                                            fileoffset = bytesUploaded.Value;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            LogWarning("Asset was not transferred as it was not found in the source web. Asset: " + sourceFileUrl, LogStrings.Heading_AssetTransfer);
            return(null);
        }
        //protected void WithExistingWebPart(ListItem listItem, WebPartDefinition webPartModel,
        //    Action<WebPart, Microsoft.SharePoint.Client.WebParts.WebPartDefinition> action)
        //{
        //    WithExistingWebPart(listItem.File, webPartModel, action);
        //}
        protected void WithExistingWebPart(File pageFile, WebPartDefinition webPartModel,
             Action<WebPart, Microsoft.SharePoint.Client.WebParts.WebPartDefinition> action)
        {
            var context = pageFile.Context;
            var webPartManager = pageFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

            // web part on the page
            var webpartOnPage = webPartManager.WebParts.Include(wp => wp.Id, wp => wp.WebPart);
            var webPartDefenitions = context.LoadQuery(webpartOnPage);

            context.ExecuteQueryWithTrace();

            Microsoft.SharePoint.Client.WebParts.WebPartDefinition def = null;
            var existingWebPart = FindExistingWebPart(webPartDefenitions, webPartModel, out def);

            action(existingWebPart, def);
        }
Beispiel #10
0
        private void ApplyFileProperties(ClientContext context, ShFileProperties fileProperties, File uploadFile)
        {
            if (fileProperties != null)
            {
                var filePropertiesWithTokensReplaced = new Dictionary <string, string>();
                foreach (KeyValuePair <string, string> keyValuePair in fileProperties.Properties)
                {
                    filePropertiesWithTokensReplaced.Add(keyValuePair.Key, ReplaceTokensInText(keyValuePair.Value, context));
                }
                uploadFile.SetFileProperties(filePropertiesWithTokensReplaced);

                if (uploadFile.Name.ToLower().EndsWith(".aspx"))
                {
                    AddWebParts(context, uploadFile, fileProperties.WebParts, fileProperties.ReplaceWebParts);
                }
                context.ExecuteQuery();
            }
        }
Beispiel #11
0
        public void InsertLibraryItem(string[] documentFields, string imagesFullPath, string imageFileName, Boolean ignoreFieldsNotFound)
        {
            #region Upload document information to SharePoint Library

            clientContext.Load(libraryList);
            clientContext.Load(libraryList.Fields);
            clientContext.ExecuteQuery();

            ArrayList arlMetadatos = new ArrayList();
            foreach (string fieldValue in documentFields)
            {
                try
                {
                    string[]  fieldValues = fieldValue.Split('|');
                    ArrayList arlValores  = new ArrayList();

                    arlValores.Add(BuscarCoincidenciaEtiqueta(fieldValues[0])); // Field Label

                    string iName = string.Empty;
                    string iType = string.Empty;
                    foreach (Field f in libraryList.Fields)
                    {
                        if (f.Title.Trim().ToUpper().Contains(arlValores[0].ToString().Trim().ToUpper()))
                        {
                            iName = f.InternalName;
                            iType = f.TypeAsString;
                            break;
                        }
                    }

                    string valor = fieldValues[1];

                    arlValores.Add(valor);          // Field Value
                    arlValores.Add(iName);          //Field Internal Name
                    arlValores.Add(iType);          //Field Type
                    arlValores.Add(fieldValues[0]); //Etiqueta
                    arlMetadatos.Add(arlValores);
                }
                catch (System.Exception ex)
                {
                    throw new System.Exception("Ocurrió un error al intentar cargar valores de metadatos, por favor verifique que los pares etiqueta|valor sean correctos." + ex.Message + " - " + ex.StackTrace);
                }
            }

            if (ignoreFieldsNotFound == false)
            {
                string msgError = "";
                foreach (ArrayList valores in arlMetadatos)
                {
                    if (valores[3].ToString() == string.Empty)
                    {
                        msgError += valores[0] + ", ";
                    }
                }

                if (msgError.Length > 0)
                {
                    throw new System.Exception("No se encontró coincidencia para los campos: " + msgError.Substring(0, msgError.Length - 2));
                }
            }

            #endregion

            #region Upload image file to SharePoint Library

            string tifImageFileNameFullPath = imagesFullPath + "\\" + imageFileName;

            var _file = new FileCreationInformation();
            _file.Content   = System.IO.File.ReadAllBytes(tifImageFileNameFullPath);
            _file.Overwrite = true;
            _file.Url       = this.mSiteURL + "/" + this.mLibraryName + "/" + imageFileName;

            Microsoft.SharePoint.Client.File uploadFile = libraryList.RootFolder.Files.Add(_file);

            #endregion

            #region Validate document metadata

            foreach (ArrayList valores in arlMetadatos)
            {
                if (valores[2].ToString() != string.Empty)
                {
                    switch (valores[3].ToString())
                    {
                    case "Text":
                        uploadFile.ListItemAllFields[valores[2].ToString()] = valores[1].ToString();
                        break;

                    case "Number":
                        if (!Util.IsNumeric(valores[1].ToString()))
                        {
                            throw new System.Exception("El valor númerico en el campo: " + valores[4].ToString() + " es inválido");
                        }

                        uploadFile.ListItemAllFields[valores[2].ToString()] = valores[1].ToString();
                        break;

                    case "DateTime":
                        DateTime valorFecha = DateTime.MinValue;
                        try
                        {
                            valorFecha = DateTime.Parse(valores[1].ToString());
                        }
                        catch
                        {
                            throw new System.Exception("El valor de fecha en el campo: " + valores[0].ToString() + " es inválido");
                        }

                        uploadFile.ListItemAllFields[valores[2].ToString()] = valorFecha;
                        break;
                    }
                }
            }

            uploadFile.ListItemAllFields["Pagina"] = _file.Url;

            uploadFile.ListItemAllFields.Update();
            clientContext.Load(uploadFile);
            clientContext.ExecuteQuery();

            #endregion
        }
        private Model.File RetrieveFieldValues(Web web, File file, Model.File homeFile)
        {
            var listItem = file.EnsureProperty(f => f.ListItemAllFields);

            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",
                "_ModerationStatus"
            };

            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;
                    if (field.TypeAsString == "URL")
                    {
                        value = Tokenize(fieldValuesAsText[fieldValue.Key], web.Url);
                    }
                    else {
                        value = Tokenize(fieldValue.Value.ToString(), web.Url);
                    }

                    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);
                        }
                    }
                    homeFile.Properties.Add(fieldValue.Key, value);
                }
            }

            return homeFile;
        }
Beispiel #13
0
        public async void InsertData()
        {
            try
            {
                var       oListData = ClientContext.Web.Lists.GetByTitle("Aftaler & dokumenter");
                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View Scope='RecursiveAll'><Query></Query></View>";
                ListItemCollection oListDataItem = oListData.GetItems(camlQuery);
                ClientContext.Load(oListDataItem);
                ClientContext.Load(oListDataItem, t => t.Include(p => p.Id, p => p.ContentType, p => p.DisplayName, p => p.FieldValuesAsText));
                ClientContext.ExecuteQuery();
                log.Info($"Aftaler & dokumenter List data pulled successfully.");
                foreach (ListItem oItem in oListDataItem)
                {
                    DocumentCategory   category;
                    List <PartnerType> partnerTypes = new List <PartnerType>();
                    try
                    {
                        log.Info($"Document data processing started Id :{oItem.GetId()}");
                        DocumentMetaData doc = new DocumentMetaData()
                        {
                            CreatedBy        = oItem.GetAuthor(),
                            CreatedDate      = oItem.GetCreateDate(),
                            LastModifiedBy   = oItem.GetEditor(),
                            LastModifiedDate = oItem.GetModifiedDate(),
                        };
                        doc.ExpirationDate = oItem.GetModifiedDate();

                        if (oItem["RelatedPartnerType"] != null)
                        {
                            var childIdField = oItem["RelatedPartnerType"] as FieldLookupValue[];

                            if (childIdField != null)
                            {
                                foreach (var lookupValue in childIdField)
                                {
                                    if (!DKBSDbContext.PartnerType.ToList().Exists(p => p.PartnerTypeTitle.ToLower() == lookupValue.LookupValue.ToLower()))
                                    {
                                        var partnerType = new PartnerType()
                                        {
                                            PartnerTypeTitle = lookupValue.LookupValue,
                                            CreatedBy        = doc.CreatedBy,
                                            CreatedDate      = doc.CreatedDate,
                                            LastModified     = doc.LastModifiedDate,
                                            LastModifiedBy   = doc.LastModifiedBy
                                        };
                                        DKBSDbContext.PartnerType.Add(partnerType);
                                        DKBSDbContext.SaveChanges();
                                        partnerTypes.Add(partnerType);
                                    }
                                    else
                                    {
                                        var partnerType = DKBSDbContext.PartnerType.FirstOrDefault(p => p.PartnerTypeTitle.ToLower() == lookupValue.LookupValue.ToLower());
                                        if (partnerType != null)
                                        {
                                            partnerTypes.Add(partnerType);
                                        }
                                    }
                                }
                            }
                        }
                        ClientContext.Load(oItem.ContentType);
                        ClientContext.ExecuteQuery();
                        if (!DKBSDbContext.DocumentCategory.ToList().Exists(p => p.CategoryName.ToLower() == oItem.ContentType.Name.ToLower()))
                        {
                            category = new Domain.DocumentCategory
                            {
                                CategoryName   = oItem.ContentType.Name,
                                CreatedBy      = doc.CreatedBy,
                                CreatedDate    = doc.CreatedDate,
                                LastModified   = doc.LastModifiedDate,
                                LastModifiedBy = doc.LastModifiedBy
                            };
                            DKBSDbContext.DocumentCategory.Add(category);
                            DKBSDbContext.SaveChanges();
                        }
                        else
                        {
                            category = DKBSDbContext.DocumentCategory.FirstOrDefault(p => p.CategoryName.ToLower() == oItem.ContentType.Name.ToLower());
                        }
                        if (oItem.FileSystemObjectType == FileSystemObjectType.File)
                        {
                            Microsoft.SharePoint.Client.File lfile = oItem.File;
                            ClientContext.Load(lfile);
                            ClientContext.ExecuteQuery();
                            doc.FileName = oItem.File.Name;
                            log.Info($"Document uploading started FileName :{doc.FileName}");
                            var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ClientContext, lfile.ServerRelativeUrl);
                            var url      = await _storageService.SaveDocumentAsync(fileInfo.Stream, doc.FileName, category.DocumentCategoryId);

                            log.Info($"Document uploaded sucessfully. started FileName :{doc.FileName} Category: {oItem.ContentType.Name}");
                        }
                        var document = new Document()
                        {
                            DocumentCategoryId = category.DocumentCategoryId,
                            ExpirationDate     = doc.ExpirationDate,
                            CreatedBy          = doc.CreatedBy,
                            CreatedDate        = doc.CreatedDate,
                            LastModified       = doc.LastModifiedDate,
                            LastModifiedBy     = doc.LastModifiedBy,
                            Details            = doc.Detail,
                            DocumentName       = doc.FileName,
                            FileName           = doc.FileName
                        };
                        foreach (var partnerType in partnerTypes)
                        {
                            var partnerTypeDoc = new PartnerTypeDocument();
                            partnerTypeDoc.DocumentId    = document.DocumentId;
                            partnerTypeDoc.PartnerTypeId = partnerType.PartnerTypeId;
                            document.PartnerTypeDocuments.Add(partnerTypeDoc);
                        }
                        DKBSDbContext.Document.Add(document);
                        DKBSDbContext.SaveChanges();
                        log.Info($"Document upload successfully Name :{doc.FileName}");
                    }
                    catch (Exception ex)
                    {
                        ErrorCount += 1;
                        log.Error($"Unable to upload file. Id:{oItem.GetId()}", ex);
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorCount += 1;
                log.Error("Unable to get documents data from sharepoint.", ex);
            }
            log.Info($"Total ErrorCount:{ErrorCount}");
        }
Beispiel #14
0
        public void InsertLibraryItem(string documentsFullPath, string imagesFullPath, string imageFileName, string documentFileName, Boolean ignoreFieldsNotFound)
        {
            #region Upload document information to SharePoint Library

            string documentFullFileName = documentsFullPath + "\\" + documentFileName;

            clientContext.Load(libraryList);
            clientContext.Load(libraryList.Fields);
            clientContext.ExecuteQuery();

            ArrayList arlMetadatos = new ArrayList();
            using (CsvFileReader reader = new CsvFileReader(documentFullFileName))
            {
                CsvRow row = new CsvRow();
                while (reader.ReadRow(row))
                {
                    //if (row[0] == "Etiqueta_36")
                    //{
                    #region Ciclo etiqueta

                    ArrayList arlEtiquetas    = new ArrayList();
                    bool      etiquetasLibros = false;
                    if (row[0] == "Etiqueta_15")
                    {
                        string x = "";
                    }

                    if (row[0] == "Etiqueta_08" || row[0] == "Etiqueta_37" || row[0] == "Etiqueta_44")
                    {
                        if (row[0] == "Etiqueta_08")
                        {
                            arlEtiquetas.Add("Etiqueta_08_01");
                            arlEtiquetas.Add("Etiqueta_08_02");
                            arlEtiquetas.Add("Etiqueta_08_03");
                        }

                        if (row[0] == "Etiqueta_37")
                        {
                            arlEtiquetas.Add("Etiqueta_37_01");
                            arlEtiquetas.Add("Etiqueta_37_02");
                            arlEtiquetas.Add("Etiqueta_37_03");
                        }

                        if (row[0] == "Etiqueta_44")
                        {
                            arlEtiquetas.Add("Etiqueta_44_01");
                            arlEtiquetas.Add("Etiqueta_44_02");
                            arlEtiquetas.Add("Etiqueta_44_03");
                        }

                        etiquetasLibros = true;
                    }
                    else
                    {
                        arlEtiquetas.Add(row[0]);
                    }

                    foreach (string etiquetaActual in arlEtiquetas)
                    {
                        ArrayList arlValores = new ArrayList();

                        arlValores.Add(BuscarCoincidenciaEtiqueta(etiquetaActual));     // Field Label

                        string iName = string.Empty;
                        string iType = string.Empty;
                        foreach (Field f in libraryList.Fields)
                        {
                            if (f.Title.Trim().ToUpper().Contains(arlValores[0].ToString().Trim().ToUpper()))
                            {
                                iName = f.InternalName;
                                iType = f.TypeAsString;
                                break;
                            }
                        }

                        string valor = "";
                        if (row.Count < 3)
                        {
                            if (etiquetaActual == "Etiqueta_31" || etiquetaActual == "Etiqueta_32" || etiquetaActual == "Etiqueta_33" || etiquetaActual == "Etiqueta_34" || etiquetaActual == "Etiqueta_25" || etiquetaActual == "Etiqueta_27" || etiquetaActual == "Etiqueta_28" || etiquetaActual == "Etiqueta_29" || etiquetaActual == "Etiqueta_30" || etiquetaActual == "Etiqueta_48")
                            {
                                valor = "";
                            }
                            else
                            {
                                throw new System.Exception("El renglon no cuenta con los campos adecuados. Etiqueta:" + row[0]);
                            }
                        }
                        else
                        {
                            valor = row[2];
                        }

                        if (etiquetasLibros)
                        {
                            valor = "";
                            if (etiquetaActual == "Etiqueta_08_01" || etiquetaActual == "Etiqueta_37_01" || etiquetaActual == "Etiqueta_44_01")
                            {
                                if (row.Count > 3)
                                {
                                    valor = row[3];
                                }
                            }

                            if (row.Count >= 5)
                            {
                                if (etiquetaActual == "Etiqueta_08_02" || etiquetaActual == "Etiqueta_37_02" || etiquetaActual == "Etiqueta_44_02")
                                {
                                    valor = row[4];
                                }

                                if (row.Count >= 7)
                                {
                                    if (etiquetaActual == "Etiqueta_08_03" || etiquetaActual == "Etiqueta_37_03" || etiquetaActual == "Etiqueta_44_03")
                                    {
                                        valor = row[6];
                                    }
                                }
                            }
                        }

                        if (iType == "Text")
                        {
                            if (row.Count > 3)
                            {
                                if (etiquetaActual != "Etiqueta_04")
                                {
                                    for (int i = 3; i < row.Count; i++)
                                    {
                                        valor += row[i].ToString();
                                    }
                                }
                            }
                        }

                        if (iType == "Number")
                        {
                            if (valor == ".")
                            {
                                valor = "0";
                            }

                            string[] tempVal = valor.Split('.');
                            if (tempVal.Length > 2)
                            {
                                valor = "";
                                for (int i = 0; i < tempVal.Length - 1; i++)
                                {
                                    valor += tempVal[i];
                                }
                                valor += "." + tempVal[tempVal.Length - 1];
                            }

                            if (!Util.IsNumeric(valor.ToString()))
                            {
                                valor = "0";
                            }
                        }

                        if (iType == "Currency")
                        {
                            if (valor == ".")
                            {
                                valor = "0";
                            }

                            string[] tempVal = valor.Split('.');
                            if (tempVal.Length > 2)
                            {
                                valor = "";
                                for (int i = 0; i < tempVal.Length - 1; i++)
                                {
                                    valor += tempVal[i];
                                }
                                valor += "." + tempVal[tempVal.Length - 1];
                            }

                            if (!Util.IsNumeric(valor.ToString()))
                            {
                                valor = "0";
                            }
                        }

                        arlValores.Add(valor);          // Field Value
                        arlValores.Add(iName);          //Field Internal Name
                        arlValores.Add(iType);          //Field Type
                        arlValores.Add(etiquetaActual); //Etiqueta
                        arlMetadatos.Add(arlValores);
                    }

                    #endregion

                    //}
                }
            }

            if (ignoreFieldsNotFound == false)
            {
                string msgError = "";
                foreach (ArrayList valores in arlMetadatos)
                {
                    if (valores[3].ToString() == string.Empty)
                    {
                        msgError += valores[0] + ", ";
                    }
                }

                if (msgError.Length > 0)
                {
                    throw new System.Exception("No se encontró coincidencia para los campos: " + msgError.Substring(0, msgError.Length - 2));
                }
            }

            #endregion

            #region Upload image file to SharePoint Library

            string tifImageFileNameFullPath = imagesFullPath + "\\" + imageFileName;

            var _file = new FileCreationInformation();
            _file.Content   = System.IO.File.ReadAllBytes(tifImageFileNameFullPath);
            _file.Overwrite = true;
            _file.Url       = this.mSiteURL + "/" + this.mLibraryName + "/" + imageFileName;

            Microsoft.SharePoint.Client.File uploadFile = libraryList.RootFolder.Files.Add(_file);


            #endregion

            #region Validate document metadata

            foreach (ArrayList valores in arlMetadatos)
            {
                if (valores[2].ToString() != string.Empty)
                {
                    switch (valores[3].ToString())
                    {
                    case "Text":
                    case "Note":
                        string tmpText = valores[1].ToString();
                        if (tmpText.Length > 250)
                        {
                            tmpText = tmpText.Substring(0, 250);
                        }
                        uploadFile.ListItemAllFields[valores[2].ToString()] = tmpText;
                        break;

                    case "Number":
                    case "Currency":
                        if (Util.IsNumeric(valores[1].ToString()))
                        {
                            uploadFile.ListItemAllFields[valores[2].ToString()] = valores[1].ToString();
                        }
                        else
                        {
                            uploadFile.ListItemAllFields[valores[2].ToString()] = "0";
                        }
                        //throw new System.Exception("El valor númerico en el campo: " + valores[4].ToString() + " del documento" + documentFileName + " es inválido");
                        break;

                    case "DateTime":
                        DateTime valorFecha = DateTime.MinValue;
                        try
                        {
                            valorFecha = DateTime.Parse(valores[1].ToString());
                        }
                        catch {
                            throw new System.Exception("El valor de fecha en el campo: " + valores[0].ToString() + " es inválido");
                        }

                        uploadFile.ListItemAllFields[valores[2].ToString()] = valorFecha;
                        break;

                    case "File":
                    case "URL":
                        break;

                    default:
                        string x1 = "";
                        break;
                    }
                }
            }

            uploadFile.ListItemAllFields["Pagina"] = _file.Url;

            //uploadFile.CheckOut();
            uploadFile.ListItemAllFields.Update();
            //uploadFile.CheckIn("", CheckinType.MajorCheckIn);

            clientContext.Load(uploadFile);

            clientContext.ExecuteQuery();

            #endregion
        }
Beispiel #15
0
        internal Model.File RetrieveFieldValues(Web web, Microsoft.SharePoint.Client.File file, Model.File modelFile)
        {
            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 = $"{{contenttypeid:{ct.Name}}}";
                            }
                        }

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

            return(modelFile);
        }
Beispiel #16
0
        public Core.File ToFile(File file)
        {
            if (file == null)
            {
                return(null);
            }

            var errorFile = file as SharePointFileErrorEntry;

            if (errorFile != null)
            {
                return new Core.File
                       {
                           ID                = MakeId(errorFile.ID),
                           FolderID          = MakeId(GetParentFolderId(errorFile.ID)),
                           CreateBy          = Owner,
                           CreateOn          = DateTime.UtcNow,
                           ModifiedBy        = Owner,
                           ModifiedOn        = DateTime.UtcNow,
                           ProviderId        = ID,
                           ProviderKey       = ProviderKey,
                           RootFolderCreator = Owner,
                           RootFolderId      = MakeId(RootFolder.ServerRelativeUrl),
                           RootFolderType    = RootFolderType,
                           Title             = MakeTitle(GetTitleById(errorFile.ID)),
                           Error             = errorFile.Error
                       }
            }
            ;

            var result = new Core.File
            {
                ID     = MakeId(file.ServerRelativeUrl),
                Access = Core.Security.FileShare.None,
                //ContentLength = file.Length,
                CreateBy          = Owner,
                CreateOn          = file.TimeCreated.Kind == DateTimeKind.Utc ? TenantUtil.DateTimeFromUtc(file.TimeCreated) : file.TimeCreated,
                FileStatus        = FileStatus.None,
                FolderID          = MakeId(GetParentFolderId(file.ServerRelativeUrl)),
                ModifiedBy        = Owner,
                ModifiedOn        = file.TimeLastModified.Kind == DateTimeKind.Utc ? TenantUtil.DateTimeFromUtc(file.TimeLastModified) : file.TimeLastModified,
                NativeAccessor    = file,
                ProviderId        = ID,
                ProviderKey       = ProviderKey,
                Title             = MakeTitle(file.Name),
                RootFolderId      = MakeId(SpRootFolderId),
                RootFolderType    = RootFolderType,
                RootFolderCreator = Owner,
                Shared            = false,
                Version           = 1
            };

            if (file.IsPropertyAvailable("Length"))
            {
                result.ContentLength = file.Length;
            }
            else if (file.IsObjectPropertyInstantiated("ListItemAllFields"))
            {
                result.ContentLength = Convert.ToInt64(file.ListItemAllFields["File_x0020_Size"]);
            }

            return(result);
        }
Beispiel #17
0
        public static void ProvisionFiles()
        {
            string        srcSiteUrl       = "http://win-f33ohjutmmi/sites/cms";
            ClientContext clientContextSRC = new ClientContext(srcSiteUrl);
            Site          srcSite          = clientContextSRC.Site;

            clientContextSRC.Load(srcSite, s => s.ServerRelativeUrl, s => s.Url);
            clientContextSRC.ExecuteQuery();

            Web srcRootWeb    = clientContextSRC.Site.RootWeb;
            Web srcCurrentWeb = clientContextSRC.Web;

            clientContextSRC.Load(srcRootWeb, rw => rw.Id);
            clientContextSRC.ExecuteQuery();
            clientContextSRC.Load(srcCurrentWeb, cw => cw.Id);
            clientContextSRC.ExecuteQuery();

            string srcMasterUrl = String.Format("{0}/_catalogs/masterpage/APCMS.master", srcSite.ServerRelativeUrl);
            File   apcmsSrcFile = null;

            string srcLayoutUrl       = String.Format("{0}/_catalogs/masterpage/BridgePage.aspx", srcSite.ServerRelativeUrl);
            File   apcmsLayoutSrcFile = null;

            string srcColourFileUrl  = String.Format("{0}/_catalogs/theme/15/PaletteAPCMS.spcolor", srcSite.ServerRelativeUrl);
            File   apcmsColorSrcFile = null;


            ClientResult <System.IO.Stream> rs       = null;
            ClientResult <System.IO.Stream> rsLayout = null;
            ClientResult <System.IO.Stream> rsColor  = null;

            if (srcRootWeb.Id.ToString() == srcCurrentWeb.Id.ToString())
            {
                //load master page and page layout
                List   masterPageGallery = srcRootWeb.Lists.GetByTitle("Master Page Gallery");
                Folder rootFolder        = masterPageGallery.RootFolder;

                apcmsSrcFile       = srcCurrentWeb.GetFileByServerRelativeUrl(srcMasterUrl);
                apcmsLayoutSrcFile = srcCurrentWeb.GetFileByServerRelativeUrl(srcLayoutUrl);

                clientContextSRC.Load(apcmsSrcFile);
                clientContextSRC.Load(apcmsLayoutSrcFile);

                clientContextSRC.ExecuteQuery();

                rs       = apcmsSrcFile.OpenBinaryStream();
                rsLayout = apcmsLayoutSrcFile.OpenBinaryStream();

                clientContextSRC.ExecuteQuery();

                //load color file
                List themeGallery = srcRootWeb.Lists.GetByTitle("Theme Gallery");
                rootFolder = themeGallery.RootFolder;

                apcmsColorSrcFile = srcCurrentWeb.GetFileByServerRelativeUrl(srcColourFileUrl);

                clientContextSRC.Load(apcmsColorSrcFile);
                clientContextSRC.ExecuteQuery();
                rsColor = apcmsColorSrcFile.OpenBinaryStream();

                clientContextSRC.ExecuteQuery();
            }


            string        siteUrl       = "http://win-f33ohjutmmi/sites/pltest";
            ClientContext clientContext = new ClientContext(siteUrl);

            Site site = clientContext.Site;

            clientContext.Load(site, s => s.ServerRelativeUrl, s => s.Url);
            clientContext.ExecuteQuery();

            Web rootWeb    = clientContext.Site.RootWeb;
            Web currentWeb = clientContext.Web;

            clientContext.Load(rootWeb, rw => rw.Id);
            clientContext.ExecuteQuery();
            clientContext.Load(currentWeb, cw => cw.Id);
            clientContext.ExecuteQuery();

            #region upload and set master page, also upload the page layout

            string masterUrl = String.Format("{0}/_catalogs/masterpage/APCMS.master", site.ServerRelativeUrl);
            string colorUrl  = String.Format("{0}/_catalogs/theme/15/PaletteAPCMS.spcolor", site.ServerRelativeUrl);


            if (rootWeb.Id.ToString() == currentWeb.Id.ToString())
            {
                List   masterPageGallery = rootWeb.Lists.GetByTitle("Master Page Gallery");
                Folder rootFolder        = masterPageGallery.RootFolder;
                //master page
                FileCreationInformation fci = new FileCreationInformation();
                fci.ContentStream = rs.Value;
                fci.Url           = "APCMS.master";
                fci.Overwrite     = true;

                Microsoft.SharePoint.Client.File fileToUpload = rootFolder.Files.Add(fci);

                clientContext.Load(fileToUpload);

                fileToUpload.Publish("");

                currentWeb.CustomMasterUrl = masterUrl;
                currentWeb.Update();
                clientContext.ExecuteQuery();

                //page layout
                fci = new FileCreationInformation();
                fci.ContentStream = rsLayout.Value;
                fci.Url           = "BridgePage.aspx";
                fci.Overwrite     = true;

                fileToUpload = rootFolder.Files.Add(fci);

                fileToUpload.Publish("");
                clientContext.ExecuteQuery();

                ListItem item = fileToUpload.ListItemAllFields;

                ContentType targetDocumentSetContentType = GetContentType(clientContext, rootWeb, "Page Layout");
                item["ContentTypeId"] = targetDocumentSetContentType.Id.ToString();
                item.Update();
                clientContext.ExecuteQuery();

                targetDocumentSetContentType            = GetContentType(clientContext, rootWeb, "Article Page");
                item["PublishingAssociatedContentType"] = String.Format(";#{0};#{1};#", targetDocumentSetContentType.Name, targetDocumentSetContentType.Id.ToString());
                item.Update();
                clientContext.ExecuteQuery();

                //color file
                List themeGallery = rootWeb.Lists.GetByTitle("Theme Gallery");
                clientContext.Load(themeGallery.RootFolder.Folders); //load the sub folder first !!!
                clientContext.ExecuteQuery();                        //must call

                rootFolder        = themeGallery.RootFolder.Folders[0];
                fci               = new FileCreationInformation();
                fci.ContentStream = rsColor.Value;
                fci.Url           = "PaletteAPCMS.spcolor";
                fci.Overwrite     = true;

                fileToUpload = rootFolder.Files.Add(fci);

                clientContext.ExecuteQuery();

                clientContext.Load(fileToUpload);

                rootWeb.ApplyTheme(colorUrl, null, null, true);
                rootWeb.Update();

                clientContext.ExecuteQuery();
            }

            #endregion
        }
        private void EnsurePublish(File file, string publishComment)
        {
            // Load dependent data.
            var parentList = file.ListItemAllFields.ParentList;
            if (!parentList.IsPropertyAvailable("EnableMinorVersion") || !parentList.IsPropertyAvailable("EnableModeration") || !file.IsPropertyAvailable("Level") || !file.IsPropertyAvailable("ListItemAllFields"))
            {
                _clientContext.Load(parentList);
                _clientContext.Load(file);
                _clientContext.ExecuteQuery();
            }

            var isDirty = false;
            if (file.Level == FileLevel.Checkout)
            {
                file.CheckIn(string.Empty, CheckinType.MajorCheckIn);
                isDirty = true;
            }
            if (parentList.EnableMinorVersions && file.Level != FileLevel.Published)
            {
                file.Publish(publishComment);
                isDirty = true;
            }
            if (parentList.EnableModeration && Convert.ToInt32(file.ListItemAllFields["_ModerationStatus"]) != 0)
            {
                file.Approve(string.Empty);
                isDirty = true;
            }

            if (isDirty)
            {
                file.RefreshLoad();
                _clientContext.ExecuteQuery();
            }
        }
Beispiel #19
0
        public void SetFileProperties(File file, IDictionary<string, string> properties, bool checkoutIfRequired = true)
        {
            var context = file.Context;
            if (properties != null && properties.Count > 0)
            {
                // Get a reference to the target list, if any
                // and load file item properties
                var parentList = file.ListItemAllFields.ParentList;
                context.Load(parentList);
                context.Load(file.ListItemAllFields);
                try
                {
                    context.ExecuteQueryRetry();
                }
                catch (ServerException ex)
                {
                    // If this throws ServerException (does not belong to list), then shouldn't be trying to set properties)
                    if (ex.Message != "The object specified does not belong to a list.")
                    {
                        throw;
                    }
                }

                // Loop through and detect changes first, then, check out if required and apply
                foreach (var kvp in properties)
                {
                    var propertyName = kvp.Key;
                    var propertyValue = kvp.Value;

                    var targetField = parentList.Fields.GetByInternalNameOrTitle(propertyName);
                    targetField.EnsureProperties(f => f.TypeAsString, f => f.ReadOnlyField);

                    // Changed by PaoloPia because there are fields like PublishingPageLayout
                    // which are marked as read-only, but have to be overwritten while uploading
                    // a publishing page file and which in reality can still be written
                    if (!targetField.ReadOnlyField || WriteableReadOnlyFields.Contains(propertyName.ToLower()))
                    {
                        switch (propertyName.ToUpperInvariant())
                        {
                            case "CONTENTTYPE":
                                {
                                    Microsoft.SharePoint.Client.ContentType targetCT = parentList.GetContentTypeByName(propertyValue);
                                    context.ExecuteQueryRetry();

                                    if (targetCT != null)
                                    {
                                        file.ListItemAllFields["ContentTypeId"] = targetCT.StringId;
                                    }
                                    else
                                    {
                                        Log.Error(Constants.LOGGING_SOURCE, "Content Type {0} does not exist in target list!", propertyValue);
                                    }
                                    break;
                                }
                            default:
                                {
                                    switch (targetField.TypeAsString)
                                    {
                                        case "User":
                                            var user = parentList.ParentWeb.EnsureUser(propertyValue);
                                            context.Load(user);
                                            context.ExecuteQueryRetry();

                                            if (user != null)
                                            {
                                                var userValue = new FieldUserValue
                                                {
                                                    LookupId = user.Id,
                                                };
                                                file.ListItemAllFields[propertyName] = userValue;
                                            }
                                            break;
                                        case "URL":
                                            var urlArray = propertyValue.Split(',');
                                            var linkValue = new FieldUrlValue();
                                            if (urlArray.Length == 2)
                                            {
                                                linkValue.Url = urlArray[0];
                                                linkValue.Description = urlArray[1];
                                            }
                                            else
                                            {
                                                linkValue.Url = urlArray[0];
                                                linkValue.Description = urlArray[0];
                                            }
                                            file.ListItemAllFields[propertyName] = linkValue;
                                            break;
                                        case "MultiChoice":
                                            var multiChoice = JsonUtility.Deserialize<String[]>(propertyValue);
                                            file.ListItemAllFields[propertyName] = multiChoice;
                                            break;
                                        case "LookupMulti":
                                            var lookupMultiValue = JsonUtility.Deserialize<FieldLookupValue[]>(propertyValue);
                                            file.ListItemAllFields[propertyName] = lookupMultiValue;
                                            break;
                                        case "TaxonomyFieldType":
                                            var taxonomyValue = JsonUtility.Deserialize<TaxonomyFieldValue>(propertyValue);
                                            file.ListItemAllFields[propertyName] = taxonomyValue;
                                            break;
                                        case "TaxonomyFieldTypeMulti":
                                            var taxonomyValueArray = JsonUtility.Deserialize<TaxonomyFieldValue[]>(propertyValue);
                                            file.ListItemAllFields[propertyName] = taxonomyValueArray;
                                            break;
                                        default:
                                            file.ListItemAllFields[propertyName] = propertyValue;
                                            break;
                                    }
                                    break;
                                }
                        }
                    }
                    file.ListItemAllFields.Update();
                    context.ExecuteQueryRetry();
                }
            }
        }
        public Core.File ToFile(File file)
        {
            if (file == null)
                return null;

            var errorFile = file as SharePointFileErrorEntry;
            if (errorFile != null)
                return new Core.File
                {
                    ID = MakeId(errorFile.ID),
                    FolderID = MakeId(GetParentFolderId(errorFile.ID)),
                    CreateBy = Owner,
                    CreateOn = DateTime.UtcNow,
                    ModifiedBy = Owner,
                    ModifiedOn = DateTime.UtcNow,
                    ProviderId = ID,
                    ProviderKey = ProviderKey,
                    RootFolderCreator = Owner,
                    RootFolderId = MakeId(RootFolder.ServerRelativeUrl),
                    RootFolderType = RootFolderType,
                    Title = MakeTitle(GetTitleById(errorFile.ID)),
                    Error = errorFile.Error
                };

            var result = new Core.File
            {
                ID = MakeId(file.ServerRelativeUrl),
                Access = Core.Security.FileShare.None,
                //ContentLength = file.Length,
                CreateBy = Owner,
                CreateOn = file.TimeCreated.Kind == DateTimeKind.Utc ? TenantUtil.DateTimeFromUtc(file.TimeCreated) : file.TimeCreated,
                FileStatus = FileStatus.None,
                FolderID = MakeId(GetParentFolderId(file.ServerRelativeUrl)),
                ModifiedBy = Owner,
                ModifiedOn = file.TimeLastModified.Kind == DateTimeKind.Utc ? TenantUtil.DateTimeFromUtc(file.TimeLastModified) : file.TimeLastModified,
                NativeAccessor = file,
                ProviderId = ID,
                ProviderKey = ProviderKey,
                Title = MakeTitle(file.Name),
                RootFolderId = MakeId(SpRootFolderId),
                RootFolderType = RootFolderType,
                RootFolderCreator = Owner,
                SharedByMe = false,
                Version = 1
            };

            if (file.IsPropertyAvailable("Length"))
            {
                result.ContentLength = file.Length;
            }
            else if(file.IsObjectPropertyInstantiated("ListItemAllFields"))
            {
                result.ContentLength = Convert.ToInt64(file.ListItemAllFields["File_x0020_Size"]);
            }

            return result;
        }
Beispiel #21
0
        public void AddWebParts(ClientContext context, File uploadFile, List<ShWebPartReference> webPartReferences, bool replaceWebParts)
        {
            // we should be allowed to delete webparts (by using replaceWebparts and define no new ones
            if (webPartReferences.Count <= 0 && !replaceWebParts) return;

            var limitedWebPartManager = uploadFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

            context.Load(limitedWebPartManager, manager => manager.WebParts);
            context.ExecuteQuery();

            if (limitedWebPartManager.WebParts.Count == 0 || replaceWebParts)
            {
                for (int i = limitedWebPartManager.WebParts.Count - 1; i >= 0; i--)
                {
                    limitedWebPartManager.WebParts[i].DeleteWebPart();
                    context.ExecuteQuery();
                }

                foreach (ShWebPartReference webPart in webPartReferences)
                {
                    //Convention: All webparts are located in the content/webparts folder
                    var webPartPath = Path.Combine(_contentDirectoryPath, "webparts", webPart.FileName);
                    var webPartFileContent = System.IO.File.ReadAllText(webPartPath);
                    if (!System.IO.File.Exists(webPartPath))
                    {
                        Log.ErrorFormat("Webpart at path {0} not found", webPartPath);
                        continue;
                    }

                    //Token replacement in the webpart XML
                    webPartFileContent = GetPropertyValueWithTokensReplaced(webPartFileContent, context);

                    //Overriding DataProviderJSON properties if specified. Need to use different approach (Update XML directly before import)
                    if (webPart.PropertiesOverrides.Count > 0 || webPart.DataProviderJSONOverrides.Count > 0)
                    {
                        webPartFileContent = ReplaceWebPartPropertyOverrides(context, webPart, webPartFileContent);
                    }

                    var webPartDefinition = limitedWebPartManager.ImportWebPart(webPartFileContent);
                    limitedWebPartManager.AddWebPart(webPartDefinition.WebPart, webPart.ZoneID, webPart.Order);
                    context.Load(limitedWebPartManager);
                    context.ExecuteQuery();
                }
            }
        }
Beispiel #22
0
        private string GetTermIdForTaxonomyField(TaxonomyField field, string term, ListItem pendingItem, Microsoft.SharePoint.Client.File pendingFile)
        {
            if (_terms == null)
            {
                _terms = new Dictionary <string, IDictionary <string, string> >();
            }

            if (!_terms.Keys.Contains(field.Title))
            {
                _terms[field.Title] = new Dictionary <string, string>();
            }

            if (_terms[field.Title].Keys.Contains(term))
            {
                return(_terms[field.Title][term].ToString());
            }

            var termId = string.Empty;

            //before we go forward,save pending item
            pendingItem.Update();
            ctx.Load(pendingFile);
            ctx.ExecuteQuery();

            TaxonomySession tSession = TaxonomySession.GetTaxonomySession(ctx);

            ctx.Load(tSession.TermStores);
            ctx.ExecuteQuery();
            TermStore ts   = tSession.TermStores.First();
            TermSet   tset = ts.GetTermSet(field.TermSetId);

            LabelMatchInformation lmi = new LabelMatchInformation(ctx);

            lmi.Lcid            = 1033;
            lmi.TrimUnavailable = true;
            lmi.TermLabel       = term;

            TermCollection termMatches = tset.GetTerms(lmi);

            ctx.Load(tSession);
            ctx.Load(ts);
            ctx.Load(tset);
            ctx.Load(termMatches);

            ctx.ExecuteQuery();

            if (termMatches != null && termMatches.Count() > 0)
            {
                termId = termMatches.First().Id.ToString();
            }

            _terms[field.Title][term] = termId;

            return(termId);
        }
Beispiel #23
0
        protected override void ExecuteCmdlet()
        {
            SourceUrl = SourceUrl ?? ServerRelativeUrl;
            var webServerRelativeUrl = SelectedWeb.EnsureProperty(w => w.ServerRelativeUrl);

            if (!SourceUrl.StartsWith("/"))
            {
                SourceUrl = UrlUtility.Combine(webServerRelativeUrl, SourceUrl);
            }
            if (!TargetUrl.StartsWith("/"))
            {
                TargetUrl = UrlUtility.Combine(webServerRelativeUrl, TargetUrl);
            }

            Uri currentContextUri = new Uri(ClientContext.Url);
            Uri sourceUri         = new Uri(currentContextUri, SourceUrl);
            Uri sourceWebUri      = Microsoft.SharePoint.Client.Web.WebUrlFromFolderUrlDirect(ClientContext, sourceUri);
            Uri targetUri         = new Uri(currentContextUri, TargetUrl);
            Uri targetWebUri      = Microsoft.SharePoint.Client.Web.WebUrlFromFolderUrlDirect(ClientContext, targetUri);

            _sourceContext = ClientContext;
            if (!currentContextUri.AbsoluteUri.Equals(sourceWebUri.AbsoluteUri, StringComparison.InvariantCultureIgnoreCase))
            {
                _sourceContext = ClientContext.Clone(sourceWebUri);
            }

            File   file   = _sourceContext.Web.GetFileByServerRelativeUrl(SourceUrl);
            Folder folder = _sourceContext.Web.GetFolderByServerRelativeUrl(SourceUrl);

            file.EnsureProperties(f => f.Name, f => f.Exists);
#if !SP2013
            folder.EnsureProperties(f => f.Name, f => f.Exists);
            bool srcIsFolder = folder.Exists;
#else
            folder.EnsureProperties(f => f.Name);
            bool srcIsFolder;
            try
            {
                folder.EnsureProperties(f => f.ItemCount); //Using ItemCount as marker if this is a file or folder
                srcIsFolder = true;
            }
            catch
            {
                srcIsFolder = false;
            }
#endif

            if (Force || ShouldContinue(string.Format(Resources.CopyFile0To1, SourceUrl, TargetUrl), Resources.Confirm))
            {
                var srcWeb = _sourceContext.Web;
                srcWeb.EnsureProperty(s => s.Url);

                _targetContext = ClientContext.Clone(targetWebUri.AbsoluteUri);
                var dstWeb = _targetContext.Web;
                dstWeb.EnsureProperty(s => s.Url);
                if (srcWeb.Url == dstWeb.Url)
                {
                    try
                    {
                        // If src/dst are on the same Web, then try using CopyTo - backwards compability
                        file.CopyTo(TargetUrl, OverwriteIfAlreadyExists);
                        _sourceContext.ExecuteQueryRetry();
                        return;
                    }
                    catch
                    {
                        SkipSourceFolderName = true; // target folder exist
                        //swallow exception, in case target was a lib/folder which exists
                    }
                }

                //different site/site collection
                Folder targetFolder       = null;
                string fileOrFolderName   = null;
                bool   targetFolderExists = false;
                try
                {
                    targetFolder = _targetContext.Web.GetFolderByServerRelativeUrl(TargetUrl);
#if !SP2013
                    targetFolder.EnsureProperties(f => f.Name, f => f.Exists);
                    if (!targetFolder.Exists)
                    {
                        throw new Exception("TargetUrl is an existing file, not folder");
                    }
                    targetFolderExists = true;
#else
                    targetFolder.EnsureProperties(f => f.Name);
                    try
                    {
                        targetFolder.EnsureProperties(f => f.ItemCount); //Using ItemCount as marker if this is a file or folder
                        targetFolderExists = true;
                    }
                    catch
                    {
                        targetFolderExists = false;
                    }
                    if (!targetFolderExists)
                    {
                        throw new Exception("TargetUrl is an existing file, not folder");
                    }
#endif
                }
                catch (Exception)
                {
                    targetFolder = null;
                    Expression <Func <List, object> > expressionRelativeUrl = l => l.RootFolder.ServerRelativeUrl;
                    var query = _targetContext.Web.Lists.IncludeWithDefaultProperties(expressionRelativeUrl);
                    var lists = _targetContext.LoadQuery(query);
                    _targetContext.ExecuteQueryRetry();
                    lists = lists.OrderByDescending(l => l.RootFolder.ServerRelativeUrl); // order descending in case more lists start with the same
                    foreach (List targetList in lists)
                    {
                        if (!TargetUrl.StartsWith(targetList.RootFolder.ServerRelativeUrl, StringComparison.InvariantCultureIgnoreCase))
                        {
                            continue;
                        }
                        fileOrFolderName = Regex.Replace(TargetUrl, targetList.RootFolder.ServerRelativeUrl, "", RegexOptions.IgnoreCase).Trim('/');
                        targetFolder     = srcIsFolder ? targetList.RootFolder.EnsureFolder(fileOrFolderName) : targetList.RootFolder;
                        break;
                    }
                }
                if (targetFolder == null)
                {
                    throw new Exception("Target does not exist");
                }
                if (srcIsFolder)
                {
                    if (!SkipSourceFolderName && targetFolderExists)
                    {
                        targetFolder = targetFolder.EnsureFolder(folder.Name);
                    }
                    CopyFolder(folder, targetFolder);
                }
                else
                {
                    UploadFile(file, targetFolder, fileOrFolderName);
                }
            }
        }
Beispiel #24
0
        /// <summary>
        /// This method adds a file to the SharePoint site.
        ///
        /// Please refer to the checkForReqProps metod for required properties.
        ///
        /// In the documentation that I found the max size of the document can be up to 3.2 meg.
        /// </summary>
        /// <param name="Interactive"></param>
        /// <returns></returns>
        public Boolean AddSPFile(bool Interactive)
        {
            Boolean       tmpRetVal = true;
            ClientContext ctx       = null;
            Web           tmpWeb    = null;
            //FileCreationInformation fcInfo = null;
            String tmpRelPath  = String.Empty;
            Folder tmpFolder   = null;
            String tmpPropList = String.Empty;

            System.Windows.Forms.Form frmReNm = new frmRenameFile();
            DialogResult tmpResult;

            //string typFldName = Properties.Settings.Default["SPDocumentTypeFieldName"].ToString();
            //string DescFldName = Properties.Settings.Default["SPDescriptionFieldName"].ToString();

            try
            {
                // Check to make sure all the required properties are set.
                if (FileOverwrite == false)
                {
                    tmpPropList = checkForReqProps("AddFile");
                }
                else
                { // If FileOverwrite is false then the AddSPFile was called by ReplaceSPFile method.
                    tmpPropList = checkForReqProps("ReplaceFile");
                }


                if (tmpPropList != String.Empty)
                {
                    tmpRetVal = false;

                    RetErrMessage = "The following Properties must be set:  " + tmpPropList;
                    return(tmpRetVal);
                }
                // See if the desired source file exists.
                if (System.IO.File.Exists(SrcFilePath))
                {
                    if (SPFileName == String.Empty)
                    {
                        SPFileName = SrcFilePath.Substring(SrcFilePath.LastIndexOf("\\") + 1);                  // get the SharePoint file name from the SrcFilePath.
                    }
                    if (AddFolderIfNeeded() == false)
                    {
                        tmpRetVal     = false;
                        RetErrMessage = "Could not add the folder: " + SPRelPath;
                        return(tmpRetVal);
                    }
                    // Check to see if the file already exists in SharePoint location.
                    if (SPFileExists())
                    {
                        if (Interactive == true)
                        {
                            // If it exists in SharePoint, give the user a chance to change the name of the file
                            //   to be saved in SharePoint.

                            // Open dialog here to get new file name
                            frmReNm.Controls["lblCurFileName"].Text = frmReNm.Controls["lblCurFileName"].Text + SPFileName;
                            frmReNm.Controls["txtNewName"].Text     = SPFileName;
                            tmpResult = frmReNm.ShowDialog();

                            switch (tmpResult)
                            {
                            case DialogResult.OK:
                                if (frmReNm.Controls["cbReplcRenam"].Text == "Replace")
                                {
                                    FileOverwrite = true;
                                }
                                // Check to see if the user has changed the file name. If changed...
                                if (frmReNm.Controls["txtNewName"].Text !=
                                    frmReNm.Controls["lblCurFileName"].Text.Substring(frmReNm.Controls["lblCurFileName"].Text.LastIndexOf(": ") + 1)
                                    )
                                {
                                    SPFileName = frmReNm.Controls["txtNewName"].Text;
                                    if (SPFileExists() && FileOverwrite == false) // check to see if the new file name exists in SharePoint.
                                    {
                                        tmpRetVal = false;
                                        MessageBox.Show("The file " + SPFileName + " already exists in SharePoint. \n Try again.", "File Already Exists", MessageBoxButtons.OK);
                                        RetErrMessage = "File already exists in SharePoint. " + SPRelPath + "/" + frmReNm.Controls["txtNewName"].Text;
                                    }
                                }
                                else
                                {
                                    tmpRetVal     = false;
                                    RetErrMessage = "File already exists in SharePoint. " + SPRelPath + "/" + frmReNm.Controls["txtNewName"].Text;
                                }
                                break;

                            case DialogResult.Cancel:
                                tmpRetVal     = false;
                                RetErrMessage = "File already exists in SharePoint. " + SPRelPath + "/" + SPFileName;
                                break;
                            }

                            frmReNm.Dispose();
                        }
                        else
                        { // Not in interactive mode and the file exists in SharePoint
                            if (FileOverwrite == false)
                            {
                                tmpRetVal     = false;
                                RetErrMessage = "File already exists in SharePoint. " + SPRelPath + "/" + SPFileName;
                            }
                        }
                    }
                    // If no errors, add the file.
                    if (tmpRetVal == true)
                    {
                        tmpRelPath = SPRelPath + "/" + WrkOrd_Job;
                        SPRelPath  = tmpRelPath;

                        ctx    = new ClientContext(SPSiteURL);
                        tmpWeb = ctx.Web;
                        ctx.Load(tmpWeb);

                        tmpFolder = tmpWeb.GetFolderByServerRelativeUrl(vSPRelPath);

                        ctx.Load(tmpFolder, f => f.ServerRelativeUrl);
                        ctx.ExecuteQuery();

                        tmpRelPath = tmpFolder.ServerRelativeUrl;

                        fs = new System.IO.FileStream(SrcFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);

                        ClientContext ctx1 = new ClientContext(SPSiteURL);
                        Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx1, tmpRelPath + "/" + SPFileName, fs, FileOverwrite);
                        Microsoft.SharePoint.Client.File tmpSPFile = ctx1.Web.GetFileByServerRelativeUrl(tmpRelPath + "/" + SPFileName);
                        tmpSPFile.ListItemAllFields[typFldName]  = SPFileType;        // Needs to be modified for Oncor
                        tmpSPFile.ListItemAllFields[DescFldName] = SPFileDescription; // Needs to be Modified for Oncor
                        tmpSPFile.ListItemAllFields.Update();
                        ctx1.Load(tmpSPFile);
                        ctx1.ExecuteQuery();

                        var tmpStr = GetCommonString(SPSiteURL, SPRelPath);
                        if (tmpStr != string.Empty)
                        {
                            RetFileURL = SPSiteURL.Replace(tmpStr, "") + vSPRelPath + "/" + SPFileName;
                        }
                        else
                        {
                            RetFileURL = SPSiteURL + vSPRelPath + "/" + SPFileName;
                        }
                        RetFileName = SPFileName;
                    }
                }
                else
                {
                    RetErrMessage = "Source File not Found.";
                    tmpRetVal     = false;
                }
            }
            catch (Exception ex)
            {
                RetErrMessage = ex.Message;
                tmpRetVal     = false;
            }

            if (null != fs)
            {
                fs.Dispose();
                fs = null;
            }

            return(tmpRetVal);
        }
        /// <summary>
        /// Transform the publishing page
        /// </summary>
        /// <param name="publishingPageTransformationInformation">Information about the publishing page to transform</param>
        /// <returns>The path to the created modern page</returns>
        public string Transform(PublishingPageTransformationInformation publishingPageTransformationInformation)
        {
            SetPageId(Guid.NewGuid().ToString());

            var logsForSettings = this.DetailSettingsAsLogEntries(publishingPageTransformationInformation);

            logsForSettings?.ForEach(o => Log(o, LogLevel.Information));

            #region Input validation
            if (publishingPageTransformationInformation.SourcePage == null)
            {
                LogError(LogStrings.Error_SourcePageNotFound, LogStrings.Heading_InputValidation);
                throw new ArgumentNullException(LogStrings.Error_SourcePageNotFound);
            }

            // Validate page and it's eligibility for transformation
            if (!publishingPageTransformationInformation.SourcePage.FieldExistsAndUsed(Constants.FileRefField) || !publishingPageTransformationInformation.SourcePage.FieldExistsAndUsed(Constants.FileLeafRefField))
            {
                LogError(LogStrings.Error_PageNotValidMissingFileRef, LogStrings.Heading_InputValidation);
                throw new ArgumentException(LogStrings.Error_PageNotValidMissingFileRef);
            }

            string pageType = publishingPageTransformationInformation.SourcePage.PageType();

            if (pageType.Equals("ClientSidePage", StringComparison.InvariantCultureIgnoreCase))
            {
                LogError(LogStrings.Error_SourcePageIsModern, LogStrings.Heading_InputValidation);
                throw new ArgumentException(LogStrings.Error_SourcePageIsModern);
            }

            if (pageType.Equals("AspxPage", StringComparison.InvariantCultureIgnoreCase))
            {
                LogError(LogStrings.Error_BasicASPXPageCannotTransform, LogStrings.Heading_InputValidation);
                throw new ArgumentException(LogStrings.Error_BasicASPXPageCannotTransform);
            }

            if (pageType.Equals("WikiPage", StringComparison.InvariantCultureIgnoreCase) || pageType.Equals("WebPartPage", StringComparison.InvariantCultureIgnoreCase))
            {
                LogError(LogStrings.Error_PageIsNotAPublishingPage, LogStrings.Heading_InputValidation);
                throw new ArgumentException(LogStrings.Error_PageIsNotAPublishingPage);
            }

            // Disable cross-farm item level permissions from copying
            CrossFarmTransformationValidation(publishingPageTransformationInformation);

            LogDebug(LogStrings.ValidationChecksComplete, LogStrings.Heading_InputValidation);
            #endregion

            try
            {
                #region Telemetry
#if DEBUG && MEASURE
                Start();
#endif
                DateTime transformationStartDateTime = DateTime.Now;

                LogDebug(LogStrings.LoadingClientContextObjects, LogStrings.Heading_SharePointConnection);
                LoadClientObject(sourceClientContext, false);

                LogInfo($"{sourceClientContext.Web.GetUrl()}", LogStrings.Heading_Summary, LogEntrySignificance.SourceSiteUrl);

                LogDebug(LogStrings.LoadingTargetClientContext, LogStrings.Heading_SharePointConnection);
                LoadClientObject(targetClientContext, true);

                SetAADTenantId(sourceClientContext, targetClientContext);

                if (sourceClientContext.Site.Id.Equals(targetClientContext.Site.Id))
                {
                    // Oops, seems source and target point to the same site collection...that's a no go for publishing portal page transformation!
                    LogError(LogStrings.Error_SameSiteTransferNoAllowedForPublishingPages, LogStrings.Heading_SharePointConnection);
                    throw new ArgumentNullException(LogStrings.Error_SameSiteTransferNoAllowedForPublishingPages);
                }

                LogInfo($"{targetClientContext.Web.GetUrl()}", LogStrings.Heading_Summary, LogEntrySignificance.TargetSiteUrl);

                // Need to add further validation for target template
                if (targetClientContext.Web.WebTemplate != "SITEPAGEPUBLISHING" && targetClientContext.Web.WebTemplate != "STS" && targetClientContext.Web.WebTemplate != "GROUP")
                {
                    LogError(LogStrings.Error_CrossSiteTransferTargetsNonModernSite);
                    throw new ArgumentException(LogStrings.Error_CrossSiteTransferTargetsNonModernSite, LogStrings.Heading_SharePointConnection);
                }

                LogInfo($"{publishingPageTransformationInformation.SourcePage[Constants.FileRefField].ToString().ToLower()}", LogStrings.Heading_Summary, LogEntrySignificance.SourcePage);

#if DEBUG && MEASURE
                Stop("Telemetry");
#endif
                #endregion

                #region Page creation
                // Detect if the page is living inside a folder
                LogDebug(LogStrings.DetectIfPageIsInFolder, LogStrings.Heading_PageCreation);
                string pageFolder = "";

                // Get the publishing pages library name
                this.publishingPagesLibraryName = CacheManager.Instance.GetPublishingPagesLibraryName(this.sourceClientContext);

                if (publishingPageTransformationInformation.SourcePage.FieldExistsAndUsed(Constants.FileDirRefField))
                {
                    var fileRefFieldValue = publishingPageTransformationInformation.SourcePage[Constants.FileDirRefField].ToString().ToLower();

                    if (fileRefFieldValue.Contains($"/{this.publishingPagesLibraryName}"))
                    {
                        string pagesLibraryRelativeUrl = $"{sourceClientContext.Web.ServerRelativeUrl.TrimEnd(new[] { '/' })}/{this.publishingPagesLibraryName}";
                        pageFolder = fileRefFieldValue.Replace(pagesLibraryRelativeUrl.ToLower(), "").Trim();
                    }
                    else
                    {
                        // Page was living in another list, leave the list name as that will be the folder hosting the modern file in SitePages.
                        // This convention is used to avoid naming conflicts
                        pageFolder = fileRefFieldValue.Replace($"{sourceClientContext.Web.ServerRelativeUrl}", "").Trim();
                    }

                    if (pageFolder.Length > 0)
                    {
                        if (pageFolder.Contains("/"))
                        {
                            if (pageFolder == "/")
                            {
                                pageFolder = "";
                            }
                            else
                            {
                                pageFolder = pageFolder.Substring(1);
                            }
                        }

                        // Add a trailing slash
                        pageFolder = pageFolder + "/";

                        LogInfo(LogStrings.PageIsLocatedInFolder, LogStrings.Heading_PageCreation);
                    }
                }
                publishingPageTransformationInformation.Folder = pageFolder;

                // If no targetname specified then we'll come up with one
                if (string.IsNullOrEmpty(publishingPageTransformationInformation.TargetPageName))
                {
                    LogInfo(LogStrings.CrossSiteInUseUsingOriginalFileName, LogStrings.Heading_PageCreation);
                    publishingPageTransformationInformation.TargetPageName = $"{publishingPageTransformationInformation.SourcePage[Constants.FileLeafRefField].ToString()}";
                }

                // Check if page name is free to use
#if DEBUG && MEASURE
                Start();
#endif
                bool           pageExists   = false;
                ClientSidePage targetPage   = null;
                List           pagesLibrary = null;
                Microsoft.SharePoint.Client.File existingFile = null;

                //The determines of the target client context has been specified and use that to generate the target page
                var context = targetClientContext;

                try
                {
                    LogDebug(LogStrings.LoadingExistingPageIfExists, LogStrings.Heading_PageCreation);

                    // Just try to load the page in the fastest possible manner, we only want to see if the page exists or not
                    existingFile = Load(sourceClientContext, targetClientContext, publishingPageTransformationInformation, out pagesLibrary);
                    pageExists   = true;
                }
                catch (Exception ex)
                {
                    if (ex is ArgumentException)
                    {
                        //Non-critical error generated
                        LogInfo(LogStrings.CheckPageExistsError, LogStrings.Heading_PageCreation);
                    }
                    else
                    {
                        //Something else occurred
                        LogError(LogStrings.CheckPageExistsError, LogStrings.Heading_PageCreation, ex);
                    }
                }

#if DEBUG && MEASURE
                Stop("Load Page");
#endif

                if (pageExists)
                {
                    LogInfo(LogStrings.PageAlreadyExistsInTargetLocation, LogStrings.Heading_PageCreation);

                    if (!publishingPageTransformationInformation.Overwrite)
                    {
                        var message = $"{LogStrings.PageNotOverwriteIfExists}  {publishingPageTransformationInformation.TargetPageName}.";
                        LogError(message, LogStrings.Heading_PageCreation);
                        throw new ArgumentException(message);
                    }
                }

                // Create the client side page
                targetPage = context.Web.AddClientSidePage($"{publishingPageTransformationInformation.Folder}{publishingPageTransformationInformation.TargetPageName}");
                LogInfo($"{LogStrings.ModernPageCreated} ", LogStrings.Heading_PageCreation);
                #endregion

                LogInfo(LogStrings.TransformSourcePageAsArticlePage, LogStrings.Heading_ArticlePageHandling);

                #region Analysis of the source page
#if DEBUG && MEASURE
                Start();
#endif
                // Analyze the source page
                Tuple <Pages.PageLayout, List <WebPartEntity> > pageData = null;

                LogInfo($"{LogStrings.TransformSourcePageIsPublishingPage} - {LogStrings.TransformSourcePageAnalysing}", LogStrings.Heading_ArticlePageHandling);

                // Grab the pagelayout mapping to use:
                var pageLayoutMappingModel = new PageLayoutManager(this.RegisteredLogObservers).GetPageLayoutMappingModel(this.publishingPageTransformation, publishingPageTransformationInformation.SourcePage);

                var spVersion = GetVersion(sourceClientContext);

                if (spVersion == SPVersion.SP2010 || spVersion == SPVersion.SP2013Legacy || spVersion == SPVersion.SP2016Legacy)
                {
                    pageData = new PublishingPageOnPremises(publishingPageTransformationInformation.SourcePage, pageTransformation, this.publishingPageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, targetContext: targetClientContext, logObservers: base.RegisteredLogObservers).Analyze(pageLayoutMappingModel);
                }
                else
                {
                    pageData = new PublishingPage(publishingPageTransformationInformation.SourcePage, pageTransformation, this.publishingPageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, targetContext: targetClientContext, logObservers: base.RegisteredLogObservers).Analyze(pageLayoutMappingModel);
                }


                // Wiki content can contain embedded images and videos, which is not supported by the target RTE...split wiki text blocks so the transformator can handle the images and videos as separate web parts
                LogInfo(LogStrings.WikiTextContainsImagesVideosReferences, LogStrings.Heading_ArticlePageHandling);
                pageData = new Tuple <Pages.PageLayout, List <WebPartEntity> >(pageData.Item1, new WikiHtmlTransformator(this.sourceClientContext, targetPage, publishingPageTransformationInformation as BaseTransformationInformation, base.RegisteredLogObservers).TransformPlusSplit(pageData.Item2, publishingPageTransformationInformation.HandleWikiImagesAndVideos, publishingPageTransformationInformation.AddTableListImageAsImageWebPart));

#if DEBUG && MEASURE
                Stop("Analyze page");
#endif
                #endregion

                #region Page title configuration
#if DEBUG && MEASURE
                Start();
#endif
                // Set page title
                SetPageTitle(publishingPageTransformationInformation, targetPage);

                if (publishingPageTransformationInformation.PageTitleOverride != null)
                {
                    var title = publishingPageTransformationInformation.PageTitleOverride(targetPage.PageTitle);
                    targetPage.PageTitle = title;

                    LogInfo($"{LogStrings.TransformPageTitleOverride} - page title: {title}", LogStrings.Heading_ArticlePageHandling);
                }
#if DEBUG && MEASURE
                Stop("Set page title");
#endif
                #endregion

                #region Page layout configuration
#if DEBUG && MEASURE
                Start();
#endif
                // Use the default layout transformator
                ILayoutTransformator layoutTransformator = new LayoutTransformator(targetPage);

                // Do we have an override?
                bool useCustomLayoutTransformator = false;
                if (publishingPageTransformationInformation.LayoutTransformatorOverride != null)
                {
                    LogInfo(LogStrings.TransformLayoutTransformatorOverride, LogStrings.Heading_ArticlePageHandling);
                    layoutTransformator          = publishingPageTransformationInformation.LayoutTransformatorOverride(targetPage);
                    useCustomLayoutTransformator = true;
                }

                // Apply the layout to the page
                layoutTransformator.Transform(pageData);

                // If needed call the specific publishing page layout transformator
                if (pageData.Item1 == Pages.PageLayout.PublishingPage_AutoDetect && !useCustomLayoutTransformator)
                {
                    // Call out the specific publishing layout transformator implementation
                    PublishingLayoutTransformator publishingLayoutTransformator = new PublishingLayoutTransformator(targetPage, base.RegisteredLogObservers);
                    publishingLayoutTransformator.Transform(pageData);
                }

#if DEBUG && MEASURE
                Stop("Page layout");
#endif
                #endregion

                #region Content transformation

                LogDebug(LogStrings.PreparingContentTransformation, LogStrings.Heading_ArticlePageHandling);

#if DEBUG && MEASURE
                Start();
#endif
                // Use the default content transformator
                IContentTransformator contentTransformator = new ContentTransformator(sourceClientContext, targetPage, pageTransformation, publishingPageTransformationInformation as BaseTransformationInformation, base.RegisteredLogObservers);

                // Do we have an override?
                if (publishingPageTransformationInformation.ContentTransformatorOverride != null)
                {
                    LogInfo(LogStrings.TransformUsingContentTransformerOverride, LogStrings.Heading_ArticlePageHandling);

                    contentTransformator = publishingPageTransformationInformation.ContentTransformatorOverride(targetPage, pageTransformation);
                }

                LogInfo(LogStrings.TransformingContentStart, LogStrings.Heading_ArticlePageHandling);

                // Run the content transformator
                contentTransformator.Transform(pageData.Item2.Where(c => !c.IsClosed).ToList());

                LogInfo(LogStrings.TransformingContentEnd, LogStrings.Heading_ArticlePageHandling);
#if DEBUG && MEASURE
                Stop("Content transformation");
#endif
                #endregion

                #region Configure header for target page
#if DEBUG && MEASURE
                Start();
#endif
                PublishingPageHeaderTransformator headerTransformator = new PublishingPageHeaderTransformator(publishingPageTransformationInformation, sourceClientContext, targetClientContext, this.publishingPageTransformation, base.RegisteredLogObservers);
                headerTransformator.TransformHeader(ref targetPage);

#if DEBUG && MEASURE
                Stop("Target page header");
#endif
                #endregion

                #region Text/Section/Column cleanup
                // Drop "empty" text parts. Wiki pages tend to have a lot of text parts just containing div's and BR's...no point in keep those as they generate to much whitespace
                RemoveEmptyTextParts(targetPage);

                // Remove empty sections and columns to optimize screen real estate
                if (publishingPageTransformationInformation.RemoveEmptySectionsAndColumns)
                {
                    RemoveEmptySectionsAndColumns(targetPage);
                }
                #endregion

                #region Page persisting + permissions

                #region Save the page
#if DEBUG && MEASURE
                Start();
#endif
                // Persist the client side page
                var pageName = $"{publishingPageTransformationInformation.Folder}{publishingPageTransformationInformation.TargetPageName}";
                targetPage.Save(pageName);
                LogInfo($"{LogStrings.TransformSavedPageInCrossSiteCollection}: {pageName}", LogStrings.Heading_ArticlePageHandling);

#if DEBUG && MEASURE
                Stop("Persist page");
#endif
                #endregion

                #region Page metadata handling
                PublishingMetadataTransformator publishingMetadataTransformator = new PublishingMetadataTransformator(publishingPageTransformationInformation, sourceClientContext, targetClientContext, targetPage, pageLayoutMappingModel, this.publishingPageTransformation, base.RegisteredLogObservers);
                publishingMetadataTransformator.Transform();
                #endregion

                #region Permission handling
                ListItemPermission listItemPermissionsToKeep = null;
                if (publishingPageTransformationInformation.KeepPageSpecificPermissions)
                {
#if DEBUG && MEASURE
                    Start();
#endif
                    // Check if we do have item level permissions we want to take over
                    listItemPermissionsToKeep = GetItemLevelPermissions(true, pagesLibrary, publishingPageTransformationInformation.SourcePage, targetPage.PageListItem);

                    // When creating the page in another site collection we'll always want to copy item level permissions if specified
                    ApplyItemLevelPermissions(true, targetPage.PageListItem, listItemPermissionsToKeep);
#if DEBUG && MEASURE
                    Stop("Permission handling");
#endif
                }
                #endregion

                #region Page Publishing
                // Tag the file with a page modernization version stamp
                string serverRelativePathForModernPage = ReturnModernPageServerRelativeUrl(publishingPageTransformationInformation);
                bool   pageListItemWasReloaded         = false;
                try
                {
                    var targetPageFile = context.Web.GetFileByServerRelativeUrl(serverRelativePathForModernPage);
                    context.Load(targetPageFile, p => p.Properties);
                    targetPageFile.Properties["sharepointpnp_pagemodernization"] = this.version;
                    targetPageFile.Update();

                    if (publishingPageTransformationInformation.PublishCreatedPage)
                    {
                        // Try to publish, if publish is not needed/possible (e.g. when no minor/major versioning set) then this will return an error that we'll be ignoring
                        targetPageFile.Publish("Page modernization initial publish");
                    }

                    // Ensure we've the most recent page list item loaded, must be last statement before calling ExecuteQuery
                    context.Load(targetPage.PageListItem);
                    // Send both the property update and publish as a single operation to SharePoint
                    context.ExecuteQueryRetry();
                    pageListItemWasReloaded = true;
                }
                catch (Exception ex)
                {
                    // Eat exceptions as this is not critical for the generated page
                    LogWarning(LogStrings.Warning_NonCriticalErrorDuringVersionStampAndPublish, LogStrings.Heading_ArticlePageHandling);
                }

                // Update flags field to indicate this is a "migrated" page
                try
                {
                    // If for some reason the reload batched with the previous request did not finish then do it again
                    if (!pageListItemWasReloaded)
                    {
                        context.Load(targetPage.PageListItem);
                        context.ExecuteQueryRetry();
                    }

                    // Only perform the update when the field was not yet set
                    bool skipSettingMigratedFromServerRendered = false;
                    if (targetPage.PageListItem[Constants.SPSitePageFlagsField] != null)
                    {
                        skipSettingMigratedFromServerRendered = (targetPage.PageListItem[Constants.SPSitePageFlagsField] as string[]).Contains("MigratedFromServerRendered");
                    }

                    if (!skipSettingMigratedFromServerRendered)
                    {
                        targetPage.PageListItem[Constants.SPSitePageFlagsField] = ";#MigratedFromServerRendered;#";
                        targetPage.PageListItem.Update();
                        context.Load(targetPage.PageListItem);
                        context.ExecuteQueryRetry();
                    }
                }
                catch (Exception ex)
                {
                    // Eat any exception
                }

                // Disable page comments on the create page, if needed
                if (publishingPageTransformationInformation.DisablePageComments)
                {
                    targetPage.DisableComments();
                    LogInfo(LogStrings.TransformDisablePageComments, LogStrings.Heading_ArticlePageHandling);
                }
                #endregion

                #region Telemetry
                if (!publishingPageTransformationInformation.SkipTelemetry && this.pageTelemetry != null)
                {
                    TimeSpan duration = DateTime.Now.Subtract(transformationStartDateTime);
                    this.pageTelemetry.LogTransformationDone(duration, pageType, publishingPageTransformationInformation);
                    this.pageTelemetry.Flush();
                }

                LogInfo(LogStrings.TransformComplete, LogStrings.Heading_PageCreation);
                #endregion

                #region Closing
                CacheManager.Instance.SetLastUsedTransformator(this);
                return(serverRelativePathForModernPage);

                #endregion

                #endregion
            }
            catch (Exception ex)
            {
                LogError(LogStrings.CriticalError_ErrorOccurred, LogStrings.Heading_Summary, ex, isCriticalException: true);
                // Throw exception if there's no registered log observers
                if (base.RegisteredLogObservers.Count == 0)
                {
                    throw;
                }
            }

            return(string.Empty);
        }
Beispiel #26
0
        private void ApplyFileProperties(ClientContext context, IEnumerable<ShFileProperties> filePropertiesCollection, File uploadFile)
        {
            if (filePropertiesCollection != null)
            {
                var fileProperties = filePropertiesCollection.SingleOrDefault(f => f.Url == uploadFile.Name);
                if (fileProperties != null)
                {
                    var filePropertiesWithTokensReplaced = new Dictionary<string, string>();
                    foreach (KeyValuePair<string, string> keyValuePair in fileProperties.Properties)
                    {
                        filePropertiesWithTokensReplaced.Add(keyValuePair.Key, GetPropertyValueWithTokensReplaced(keyValuePair.Value, context));
                    }
                    uploadFile.SetFileProperties(filePropertiesWithTokensReplaced);

                    if (uploadFile.Name.ToLower().EndsWith(".aspx")) 
                        AddWebParts(context, uploadFile, fileProperties.WebParts, fileProperties.ReplaceWebParts);
                    context.ExecuteQuery();
                }
            }
        }
Beispiel #27
0
        protected override void ExecuteCmdlet()
        {
            //Fix loading of modernization framework
            FixLocalAssemblyResolving();

            // Load the page to transform
            Identity.Library  = this.Library;
            Identity.Folder   = this.Folder;
            Identity.BlogPage = this.BlogPage;

            if (this.PublishingPage && this.BlogPage)
            {
                throw new Exception($"The page is either a blog page or a publishing page or not of them...setting both PublishingPage and BlogPage to true is not valid.");
            }

            ListItem page = null;

            if (this.PublishingPage)
            {
                page = Identity.GetPage(this.ClientContext.Web, CacheManager.Instance.GetPublishingPagesLibraryName(this.ClientContext));
            }
            else if (this.BlogPage)
            {
                // Blogs don't live in other libraries or sub folders
                Identity.Library = null;
                Identity.Folder  = null;
                page             = Identity.GetPage(this.ClientContext.Web, CacheManager.Instance.GetBlogListName(this.ClientContext));
            }
            else
            {
                if (this.Folder == null || !this.Folder.Equals(rootFolder, StringComparison.InvariantCultureIgnoreCase))
                {
                    page = Identity.GetPage(this.ClientContext.Web, "sitepages");
                }
            }

            if (page == null && !this.Folder.Equals(rootFolder, StringComparison.InvariantCultureIgnoreCase))
            {
                throw new Exception($"Page '{Identity?.Name}' does not exist");
            }

            // Publishing specific validation
            if (this.PublishingPage && string.IsNullOrEmpty(this.TargetWebUrl) && TargetConnection == null)
            {
                throw new Exception($"Publishing page transformation is only supported when transformating into another site collection. Use the -TargetWebUrl to specify a modern target site.");
            }

            // Load transformation models
            PageTransformation webPartMappingModel = null;

            if (string.IsNullOrEmpty(this.WebPartMappingFile))
            {
                // Load xml mapping data
                XmlSerializer xmlMapping = new XmlSerializer(typeof(PageTransformation));

                // Load the default one from resources into a model, no need for persisting this file
                string webpartMappingFileContents = WebPartMappingLoader.LoadFile("SharePointPnP.PowerShell.Commands.ClientSidePages.webpartmapping.xml");

                using (var stream = GenerateStreamFromString(webpartMappingFileContents))
                {
                    webPartMappingModel = (PageTransformation)xmlMapping.Deserialize(stream);
                }

                this.WriteVerbose("Using embedded webpartmapping file. Use Export-PnPClientSidePageMapping to get that file in case you want to base your version of the embedded version.");
            }

            // Validate webpartmappingfile
            if (!string.IsNullOrEmpty(this.WebPartMappingFile))
            {
                if (!System.IO.File.Exists(this.WebPartMappingFile))
                {
                    throw new Exception($"Provided webpartmapping file {this.WebPartMappingFile} does not exist");
                }
            }

            if (this.PublishingPage && !string.IsNullOrEmpty(this.PageLayoutMapping) && !System.IO.File.Exists(this.PageLayoutMapping))
            {
                throw new Exception($"Provided pagelayout mapping file {this.PageLayoutMapping} does not exist");
            }

            bool crossSiteTransformation = TargetConnection != null || !string.IsNullOrEmpty(TargetWebUrl);

            // Create target client context (when needed)
            ClientContext targetContext = null;

            if (TargetConnection == null)
            {
                if (!string.IsNullOrEmpty(TargetWebUrl))
                {
                    targetContext = this.ClientContext.Clone(TargetWebUrl);
                }
            }
            else
            {
                targetContext = TargetConnection.Context;
            }


            // Create transformator instance
            PageTransformator           pageTransformator           = null;
            PublishingPageTransformator publishingPageTransformator = null;

            if (!string.IsNullOrEmpty(this.WebPartMappingFile))
            {
                // Using custom web part mapping file
                if (this.PublishingPage)
                {
                    if (!string.IsNullOrEmpty(this.PageLayoutMapping))
                    {
                        // Using custom page layout mapping file + default one (they're merged together)
                        publishingPageTransformator = new PublishingPageTransformator(this.ClientContext, targetContext, this.WebPartMappingFile, this.PageLayoutMapping);
                    }
                    else
                    {
                        // Using default page layout mapping file
                        publishingPageTransformator = new PublishingPageTransformator(this.ClientContext, targetContext, this.WebPartMappingFile, null);
                    }
                }
                else
                {
                    // Use web part mapping file
                    pageTransformator = new PageTransformator(this.ClientContext, targetContext, this.WebPartMappingFile);
                }
            }
            else
            {
                // Using default web part mapping file
                if (this.PublishingPage)
                {
                    if (!string.IsNullOrEmpty(this.PageLayoutMapping))
                    {
                        // Load and validate the custom mapping file
                        PageLayoutManager pageLayoutManager = new PageLayoutManager();
                        var pageLayoutMappingModel          = pageLayoutManager.LoadPageLayoutMappingFile(this.PageLayoutMapping);

                        // Using custom page layout mapping file + default one (they're merged together)
                        publishingPageTransformator = new PublishingPageTransformator(this.ClientContext, targetContext, webPartMappingModel, pageLayoutMappingModel);
                    }
                    else
                    {
                        // Using default page layout mapping file
                        publishingPageTransformator = new PublishingPageTransformator(this.ClientContext, targetContext, webPartMappingModel, null);
                    }
                }
                else
                {
                    // Use web part mapping model loaded from embedded mapping file
                    pageTransformator = new PageTransformator(this.ClientContext, targetContext, webPartMappingModel);
                }
            }

            // Setup logging
            if (this.LogType == ClientSidePageTransformatorLogType.File)
            {
                if (this.PublishingPage)
                {
                    publishingPageTransformator.RegisterObserver(new MarkdownObserver(folder: this.LogFolder, includeVerbose: this.LogVerbose, includeDebugEntries: this.LogVerbose));
                }
                else
                {
                    pageTransformator.RegisterObserver(new MarkdownObserver(folder: this.LogFolder, includeVerbose: this.LogVerbose, includeDebugEntries: this.LogVerbose));
                }
            }
            else if (this.LogType == ClientSidePageTransformatorLogType.SharePoint)
            {
                if (this.PublishingPage)
                {
                    publishingPageTransformator.RegisterObserver(new MarkdownToSharePointObserver(targetContext ?? this.ClientContext, includeVerbose: this.LogVerbose, includeDebugEntries: this.LogVerbose));
                }
                else
                {
                    pageTransformator.RegisterObserver(new MarkdownToSharePointObserver(targetContext ?? this.ClientContext, includeVerbose: this.LogVerbose, includeDebugEntries: this.LogVerbose));
                }
            }
            else if (this.LogType == ClientSidePageTransformatorLogType.Console)
            {
                if (this.PublishingPage)
                {
                    publishingPageTransformator.RegisterObserver(new ConsoleObserver(includeDebugEntries: this.LogVerbose));
                }
                else
                {
                    pageTransformator.RegisterObserver(new ConsoleObserver(includeDebugEntries: this.LogVerbose));
                }
            }

            // Clear the client side component cache
            if (this.ClearCache)
            {
                CacheManager.Instance.ClearAllCaches();
            }

            string serverRelativeClientPageUrl = "";

            if (this.PublishingPage)
            {
                // Setup Transformation information
                PublishingPageTransformationInformation pti = new PublishingPageTransformationInformation(page)
                {
                    Overwrite = this.Overwrite,
                    KeepPageSpecificPermissions             = !this.SkipItemLevelPermissionCopyToClientSidePage,
                    PublishCreatedPage                      = !this.DontPublish,
                    KeepPageCreationModificationInformation = this.KeepPageCreationModificationInformation,
                    PostAsNews                      = this.PostAsNews,
                    DisablePageComments             = this.DisablePageComments,
                    TargetPageName                  = !string.IsNullOrEmpty(this.PublishingTargetPageName) ? this.PublishingTargetPageName : this.TargetPageName,
                    SkipUrlRewrite                  = this.SkipUrlRewriting,
                    SkipDefaultUrlRewrite           = this.SkipDefaultUrlRewriting,
                    UrlMappingFile                  = this.UrlMappingFile,
                    AddTableListImageAsImageWebPart = this.AddTableListImageAsImageWebPart,
                    SkipUserMapping                 = this.SkipUserMapping,
                    UserMappingFile                 = this.UserMappingFile,
                    LDAPConnectionString            = this.LDAPConnectionString,
                    TargetPageFolder                = this.TargetPageFolder,
                };

                // Set mapping properties
                pti.MappingProperties["SummaryLinksToQuickLinks"] = (!SummaryLinksToHtml).ToString().ToLower();
                pti.MappingProperties["UseCommunityScriptEditor"] = UseCommunityScriptEditor.ToString().ToLower();

                try
                {
                    serverRelativeClientPageUrl = publishingPageTransformator.Transform(pti);
                }
                finally
                {
                    // Flush log
                    if (this.LogType != ClientSidePageTransformatorLogType.None && this.LogType != ClientSidePageTransformatorLogType.Console && !this.LogSkipFlush)
                    {
                        publishingPageTransformator.FlushObservers();
                    }
                }
            }
            else
            {
                Microsoft.SharePoint.Client.File fileToModernize = null;
                if (this.Folder != null && this.Folder.Equals(rootFolder, StringComparison.InvariantCultureIgnoreCase))
                {
                    // Load the page file from the site root folder
                    var webServerRelativeUrl = this.ClientContext.Web.EnsureProperty(p => p.ServerRelativeUrl);
                    fileToModernize = this.ClientContext.Web.GetFileByServerRelativeUrl($"{webServerRelativeUrl}/{this.Identity.Name}");
                    this.ClientContext.Load(fileToModernize);
                    this.ClientContext.ExecuteQueryRetry();
                }

                // Setup Transformation information
                PageTransformationInformation pti = new PageTransformationInformation(page)
                {
                    SourceFile = fileToModernize,
                    Overwrite  = this.Overwrite,
                    TargetPageTakesSourcePageName      = this.TakeSourcePageName,
                    ReplaceHomePageWithDefaultHomePage = this.ReplaceHomePageWithDefault,
                    KeepPageSpecificPermissions        = !this.SkipItemLevelPermissionCopyToClientSidePage,
                    CopyPageMetadata   = this.CopyPageMetadata,
                    PublishCreatedPage = !this.DontPublish,
                    KeepPageCreationModificationInformation = this.KeepPageCreationModificationInformation,
                    SetAuthorInPageHeader           = this.SetAuthorInPageHeader,
                    PostAsNews                      = this.PostAsNews,
                    DisablePageComments             = this.DisablePageComments,
                    TargetPageName                  = crossSiteTransformation ? this.TargetPageName : "",
                    SkipUrlRewrite                  = this.SkipUrlRewriting,
                    SkipDefaultUrlRewrite           = this.SkipDefaultUrlRewriting,
                    UrlMappingFile                  = this.UrlMappingFile,
                    AddTableListImageAsImageWebPart = this.AddTableListImageAsImageWebPart,
                    SkipUserMapping                 = this.SkipUserMapping,
                    UserMappingFile                 = this.UserMappingFile,
                    LDAPConnectionString            = this.LDAPConnectionString,
                    TargetPageFolder                = this.TargetPageFolder,
                    ModernizationCenterInformation  = new ModernizationCenterInformation()
                    {
                        AddPageAcceptBanner = this.AddPageAcceptBanner
                    },
                };

                // Set mapping properties
                pti.MappingProperties["SummaryLinksToQuickLinks"] = (!SummaryLinksToHtml).ToString().ToLower();
                pti.MappingProperties["UseCommunityScriptEditor"] = UseCommunityScriptEditor.ToString().ToLower();

                try
                {
                    serverRelativeClientPageUrl = pageTransformator.Transform(pti);
                }
                finally
                {
                    // Flush log
                    if (this.LogType != ClientSidePageTransformatorLogType.None && this.LogType != ClientSidePageTransformatorLogType.Console && !this.LogSkipFlush)
                    {
                        pageTransformator.FlushObservers();
                    }
                }
            }

            // Output the server relative url to the newly created page
            if (!string.IsNullOrEmpty(serverRelativeClientPageUrl))
            {
                WriteObject(serverRelativeClientPageUrl);
            }
        }
Beispiel #28
0
        private static bool CheckOutIfNeeded(Web web, File targetFile)
        {
            var checkedOut = false;
            try
            {
                web.Context.Load(targetFile, f => f.CheckOutType, f => f.ListItemAllFields.ParentList.ForceCheckout);
                web.Context.ExecuteQueryRetry();

                if (targetFile.CheckOutType == CheckOutType.None)
                {
                    targetFile.CheckOut();
                }
                checkedOut = true;
            }
            catch (ServerException ex)
            {
                // Handling the exception stating the "The object specified does not belong to a list."
                if (ex.ServerErrorCode != -2146232832)
                {
                    throw;
                }
            }
            return checkedOut;
        }
        public void CleanUp()
        {
            Console.WriteLine("BrandingExtensionsTests.CleanUp");

            if (System.IO.File.Exists(customColorFilePath))
            {
                System.IO.File.Delete(customColorFilePath);
            }
            if (System.IO.File.Exists(customBackgroundFilePath))
            {
                System.IO.File.Delete(customBackgroundFilePath);
            }

            using (var context = TestCommon.CreateClientContext())
            {
                var web = context.Web;

                // Remove composed looks from server
                List      themeGallery = web.GetCatalog((int)ListTemplateType.DesignCatalog);
                CamlQuery query        = new CamlQuery();
                string    camlString   = @"
                    <View>
                        <Query>                
                            <Where>
                                <Contains>
                                    <FieldRef Name='Name' />
                                    <Value Type='Text'>Test_</Value>
                                </Contains>
                            </Where>
                        </Query>
                    </View>";
                query.ViewXml = camlString;
                var found = themeGallery.GetItems(query);
                web.Context.Load(found);
                web.Context.ExecuteQueryRetry();
                Console.WriteLine("{0} matching looks found to delete", found.Count);
                var looksToDelete = found.ToList();
                foreach (var item in looksToDelete)
                {
                    Console.WriteLine("Delete look item '{0}'", item["Name"]);
                    item.DeleteObject();
                    context.ExecuteQueryRetry();
                }

                // Remove Theme Files
                List             themesList  = web.GetCatalog((int)ListTemplateType.ThemeCatalog);
                Folder           rootFolder  = themesList.RootFolder;
                FolderCollection rootFolders = rootFolder.Folders;
                web.Context.Load(rootFolder);
                web.Context.Load(rootFolders, f => f.Where(folder => folder.Name == "15"));
                web.Context.ExecuteQueryRetry();

                Folder folder15 = rootFolders.FirstOrDefault();

                try
                {
                    Microsoft.SharePoint.Client.File customColorFile = folder15.Files.GetByUrl("custom.spcolor");
                    customColorFile.DeleteObject();
                    context.ExecuteQueryRetry();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception cleaning up: {0}", ex);
                }

                try
                {
                    Microsoft.SharePoint.Client.File customBackgroundFile = folder15.Files.GetByUrl("custombg.jpg");
                    customBackgroundFile.DeleteObject();
                    context.ExecuteQueryRetry();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception cleaning up: {0}", ex);
                }

                // Remove webs
                var webCollection1 = web.Webs;
                context.Load(webCollection1, wc => wc.Include(w => w.Title, w => w.ServerRelativeUrl));
                context.ExecuteQueryRetry();
                var websToDelete = new List <Web>();
                foreach (var web1 in webCollection1)
                {
                    if (web1.Title.StartsWith("Test_"))
                    {
                        var webCollection2 = web1.Webs;
                        context.Load(webCollection2, wc => wc.Include(w => w.Title, w => w.ServerRelativeUrl));
                        context.ExecuteQueryRetry();
                        var childrenToDelete = new List <Web>(webCollection2);
                        foreach (var web2 in childrenToDelete)
                        {
                            Console.WriteLine("Deleting site {0}", web2.ServerRelativeUrl);
                            web2.DeleteObject();
                            context.ExecuteQueryRetry();
                        }
                        websToDelete.Add(web1);
                    }
                }

                foreach (var web1 in websToDelete)
                {
                    Console.WriteLine("Deleting site {0}", web1.ServerRelativeUrl);
                    web1.DeleteObject();
                    try
                    {
                        context.ExecuteQueryRetry();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Exception cleaning up: {0}", ex);
                    }
                }

                // Remove pagelayouts
                List   masterPageGallery             = context.Web.GetCatalog((int)ListTemplateType.MasterPageCatalog);
                Folder rootFolderInMasterPageGallery = masterPageGallery.RootFolder;
                context.Load(rootFolderInMasterPageGallery, f => f.ServerRelativeUrl);
                context.ExecuteQueryRetry();

                try
                {
                    var fileServerRelativeUrl = UrlUtility.Combine(rootFolderInMasterPageGallery.ServerRelativeUrl, publishingPageWithoutExtension);
                    var file = context.Web.GetFileByServerRelativeUrl(String.Format("{0}.aspx", fileServerRelativeUrl));
                    context.Load(file);
                    context.ExecuteQueryRetry();
                    file.DeleteObject();
                    context.ExecuteQueryRetry();

                    fileServerRelativeUrl = UrlUtility.Combine(rootFolderInMasterPageGallery.ServerRelativeUrl, "test/test", publishingPageWithoutExtension);
                    file = context.Web.GetFileByServerRelativeUrl(String.Format("{0}.aspx", fileServerRelativeUrl));
                    context.Load(file);
                    context.ExecuteQueryRetry();
                    file.DeleteObject();
                    context.ExecuteQueryRetry();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception cleaning up: {0}", ex);
                }
            }

            Teardown();
        }