Пример #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();
        }
        protected FieldLink FindFieldLinkById(FieldLinkCollection fiedLinks, Guid fieldId)
        {
            foreach (var fieldLink in fiedLinks)
            {
                if (fieldLink.Id == fieldId)
                    return fieldLink;
            }

            return null;
        }
        private FieldLink FindContentTypeFieldLink(FieldLinkCollection fieldLinks, Guid guid)
        {
            foreach (var fieldlink in fieldLinks)
            {
                if (fieldlink.Id == guid)
                    return fieldlink;
            }

            return null;
        }
Пример #4
0
        public static void CreateContentType(ClientContext ctx, string contentTypeName, string categoryFieldName)
        {
            //ctx.Web.ContentTypeExistsByName
            ContentTypeCollection contentTypes = ctx.Web.ContentTypes;

            ctx.Load(contentTypes);
            ctx.ExecuteQuery();
            if (ctx.Web.ContentTypeExistsByName(contentTypeName))
            {
                return;
            }


            // Create a Content Type Information object.
            ContentTypeCreationInformation newCt = new ContentTypeCreationInformation();

            // Set the name for the content type.
            newCt.Name = contentTypeName;


            //Site Page - 0x0101009D1CB255DA76424F860D91F20E6C4118
            newCt.ParentContentType = ctx.Web.ContentTypes.GetById("0x0101009D1CB255DA76424F860D91F20E6C4118");

            // Set content type to be available from specific group.
            newCt.Group = "LB Content Types";


            // Create the content type.
            Microsoft.SharePoint.Client.ContentType myContentType = contentTypes.Add(newCt);

            FieldLinkCollection fieldsCollection = myContentType.FieldLinks;

            ctx.Load(fieldsCollection);
            ctx.ExecuteQuery();



            FieldCollection fields = ctx.Site.RootWeb.Fields;

            ctx.Load(fields);
            ctx.ExecuteQuery();


            //Field f = ctx.Site.RootWeb.Fields.GetFieldByInternalName(categoryFieldName);
            fieldsCollection.Add(new FieldLinkCreationInformation
            {
                //Field = ctx.Site.RootWeb.Fields.GetFieldByInternalName(categoryFieldName)
                Field = fields.GetFieldByInternalName(categoryFieldName)
            });

            myContentType.Update(true);
            ctx.ExecuteQuery();
        }
        protected FieldLink FindFieldLinkById(FieldLinkCollection fiedLinks, Guid fieldId)
        {
            foreach (var fieldLink in fiedLinks)
            {
                if (fieldLink.Id == fieldId)
                {
                    return(fieldLink);
                }
            }

            return(null);
        }
        private FieldLink FindContentTypeFieldLink(FieldLinkCollection fieldLinks, Guid guid)
        {
            foreach (var fieldlink in fieldLinks)
            {
                if (fieldlink.Id == guid)
                {
                    return(fieldlink);
                }
            }

            return(null);
        }
Пример #7
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 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();
            }
        }
Пример #8
0
        static void Main(string[] args)
        {
            string userName = "******";

            Console.WriteLine("Enter your password.");
            SecureString password = getpassword();

            using (var ctx = new ClientContext("https://sites.bms.com/sites/miketest"))
            {
                ctx.Credentials = new SharePointOnlineCredentials(userName, password);
                Web subsite = ctx.Web;
                ctx.Load(subsite);
                ctx.ExecuteQuery();
                List userlist = subsite.Lists.GetByTitle("Clinical");
                ctx.Load(subsite);
                ctx.ExecuteQuery();
                ContentType ct = userlist.ContentTypes.GetById("0x010047A9C2CCA715F64E96F1FCA3603F845A");
                ctx.Load(ct);
                ctx.ExecuteQuery();
                Field column = userlist.Fields.GetByInternalNameOrTitle("console4");
                ctx.Load(column);
                ctx.ExecuteQuery();
                Console.WriteLine("Title: " + subsite.Title + "; URL: " + subsite.Url + "; column: " + column.InternalName);
                Console.ReadLine();
                FieldLinkCollection addcolumntoct = ct.FieldLinks;
                ctx.Load(addcolumntoct);
                ctx.ExecuteQuery();
                foreach (var item in addcolumntoct)
                {
                    if (item.Name == "console4")
                    {
                        return;
                    }
                }
                FieldLinkCreationInformation link = new FieldLinkCreationInformation();
                link.Field = column;
                addcolumntoct.Add(link);
                ct.Update(false);
                ctx.ExecuteQuery();
            }
        }
Пример #9
0
        private void RemoveFieldFromContentType(FieldCollection webFields, string fieldNameToRemove,
                                                ContentType contentType)
        {
            // Need to load content type fields every iteration because fields are added to the collection
            Field webField = webFields.GetByInternalNameOrTitle(fieldNameToRemove);
            FieldLinkCollection contentTypeFields = contentType.FieldLinks;

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

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

            if (fieldLink != null)
            {
                fieldLink.DeleteObject();
            }
            contentType.Update(true);
            ClientContext.ExecuteQuery();
        }
Пример #10
0
        private static void AddSiteColumnToContentType(ClientContext cc, Web web)
        {
            ContentTypeCollection contentTypes = web.ContentTypes;

            cc.Load(contentTypes);
            cc.ExecuteQuery();
            ContentType myContentType = contentTypes.GetById("0x0101009189AB5D3D2647B580F011DA2F356FB2");

            cc.Load(myContentType);
            cc.ExecuteQuery();

            FieldCollection fields = web.Fields;
            Field           fld    = fields.GetByInternalNameOrTitle("ContosoString");

            cc.Load(fields);
            cc.Load(fld);
            cc.ExecuteQuery();

            FieldLinkCollection refFields = myContentType.FieldLinks;

            cc.Load(refFields);
            cc.ExecuteQuery();

            foreach (var item in refFields)
            {
                if (item.Name == "ContosoString")
                {
                    return;
                }
            }

            // ref does nt
            FieldLinkCreationInformation link = new FieldLinkCreationInformation();

            link.Field = fld;
            myContentType.FieldLinks.Add(link);
            myContentType.Update(true);
            cc.ExecuteQuery();
        }
Пример #11
0
        private void AddSiteColumnsToContentType(GtContentType configContentType)
        {
            Web web = ClientContext.Web;
            ContentTypeCollection contentTypes = web.ContentTypes;

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

            ClientContext.Load(contentType);
            ClientContext.Load(fields);
            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 = fields.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();
            }
        }