Beispiel #1
0
        private void AddOrUpdateFieldOfContentType(ShContentType configContentType, FieldCollection webFields,
                                                   string fieldName,
                                                   ContentType contentType)
        {
            // Need to load content type fields every iteration because fields are added to the collection
            Field webField = webFields.GetByInternalNameOrTitle(fieldName);
            FieldLinkCollection contentTypeFields = contentType.FieldLinks;

            ClientContext.Load(contentTypeFields);
            ClientContext.Load(webField);
            ClientContext.ExecuteQuery();

            var fieldLink = contentTypeFields.FirstOrDefault(existingFieldName => existingFieldName.Name == fieldName);

            if (fieldLink == null)
            {
                var link = new FieldLinkCreationInformation {
                    Field = webField
                };
                fieldLink = contentType.FieldLinks.Add(link);
            }

            fieldLink.Required = configContentType.RequiredFields.Contains(fieldName);
            fieldLink.Hidden   = configContentType.HiddenFields.Contains(fieldName);

            contentType.Update(true);
            ClientContext.ExecuteQuery();
        }
        private void AddSiteColumnsToContentType(ShContentType configContentType)
        {
            Log.Debug("Attempting to add fields to content type " + configContentType.DisplayName);

            Web web = ClientContext.Web;
            ContentTypeCollection contentTypes = web.ContentTypes;

            ClientContext.Load(contentTypes);
            ClientContext.ExecuteQuery();
            ContentType     contentType = contentTypes.GetById(configContentType.ID);
            FieldCollection webFields   = web.Fields;

            ClientContext.Load(contentType);
            ClientContext.Load(webFields);
            ClientContext.ExecuteQuery();

            foreach (var fieldName in configContentType.Fields)
            {
                // Need to load content type fields every iteration because fields are added to the collection
                Field webField = webFields.GetByInternalNameOrTitle(fieldName);
                FieldLinkCollection contentTypeFields = contentType.FieldLinks;
                ClientContext.Load(contentTypeFields);
                ClientContext.Load(webField);
                ClientContext.ExecuteQuery();

                var fieldLink = contentTypeFields.FirstOrDefault(existingFieldName => existingFieldName.Name == fieldName);
                if (fieldLink == null)
                {
                    var link = new FieldLinkCreationInformation {
                        Field = webField
                    };
                    fieldLink = contentType.FieldLinks.Add(link);
                }

                fieldLink.Required = configContentType.RequiredFields.Contains(fieldName);
                if (configContentType.HiddenFields.Contains(fieldName))
                {
                    fieldLink.Hidden   = true;
                    fieldLink.Required = false;
                    //var hiddenField = contentType.Fields.FirstOrDefault(ct => ct.InternalName == fieldName);
                    //if (hiddenField != null)
                    //{
                    //    hiddenField.Required = false;
                    //    hiddenField.Update();
                    //}
                }
                contentType.Update(true);
                ClientContext.ExecuteQuery();
            }
        }
Beispiel #3
0
        private void AddTemplateToContentType(ShContentType configContentType)
        {
            Log.Debug("Attempting to add fields to content type " + configContentType.DisplayName);
            Web web = ClientContext.Web;
            //ContentTypeCollection contentTypes = web.ContentTypes;
            //ClientContext.Load(contentTypes);
            //// ClientContext.ExecuteQuery();
            //ContentType contentType = contentTypes.GetById(configContentType.ID);
            //ClientContext.Load(contentType);
            //ClientContext.ExecuteQuery();

            ContentType ct = web.ContentTypes.GetById(configContentType.ID);

            ClientContext.Load(ct);
            ClientContext.ExecuteQuery();

            // Get instance to the _cts folder created for the each of the content types
            string ctFolderServerRelativeURL = "_cts/" + configContentType.InternalName;
            Folder ctFolder = web.GetFolderByServerRelativeUrl(ctFolderServerRelativeURL);

            ClientContext.Load(ctFolder);
            ClientContext.ExecuteQuery();

            // Load the local template document
            string path     = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "templates", configContentType.Template.FileName);
            string fileName = System.IO.Path.GetFileName(path);

            byte[] filecontent = System.IO.File.ReadAllBytes(path);

            // Uplaod file to the Office365
            using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open))
            {
                FileCreationInformation newFile = new FileCreationInformation();
                newFile.Content   = filecontent;
                newFile.Url       = ctFolderServerRelativeURL + "/" + fileName;
                newFile.Overwrite = true;

                Microsoft.SharePoint.Client.File uploadedFile = ctFolder.Files.Add(newFile);
                ClientContext.Load(uploadedFile);
                ClientContext.ExecuteQuery();
            }

            ct.DocumentTemplate = fileName;
            ct.Update(true);
            ClientContext.ExecuteQuery();
            Log.Debug("Document template uploaded and set to the content type.");

            //SetDocumentAsTemplate(cc, web, "0x010100293fde3fcada480b9a77bbdad7dfa28c0105", "template.docx", "BBProjectMeeting");
        }
Beispiel #4
0
        private void AddSiteColumnsToContentType(ShContentType configContentType)
        {
            Log.Debug("Attempting to add fields to content type " + configContentType.DisplayName);

            Web web = ClientContext.Web;
            ContentTypeCollection contentTypes = web.ContentTypes;

            ClientContext.Load(contentTypes);
            ClientContext.ExecuteQuery();
            ContentType     contentType = contentTypes.GetById(configContentType.ID);
            FieldCollection webFields   = web.Fields;

            ClientContext.Load(contentType);
            ClientContext.Load(webFields);
            ClientContext.ExecuteQuery();

            foreach (var fieldNameToRemove in configContentType.RemovedFields)
            {
                try
                {
                    RemoveFieldFromContentType(webFields, fieldNameToRemove, contentType);
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Field {0} could not be removed from the content type {1} with error {2}",
                                    fieldNameToRemove, configContentType.DisplayName, ex.Message);
                }
            }
            foreach (var fieldName in configContentType.Fields)
            {
                // We don't want to add removed fields to the content type
                if (configContentType.RemovedFields.Contains(fieldName))
                {
                    continue;
                }

                try
                {
                    AddOrUpdateFieldOfContentType(configContentType, webFields, fieldName, contentType);
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Field {0} could not be added to the content type {1} with error {2}", fieldName,
                                    configContentType.DisplayName, ex.Message);
                    Log.Info("Field " + fieldName +
                             " does not exist. If this is a lookup field, run content type creation again after setting up site hierarchy");
                }
            }
        }