Пример #1
0
        private static void CreatePages(ClientContext context, EnterpriseManualProperies p, string targetContentTypeName)
        {
            var page = context.Web.AddClientSidePage(p.FileName, true);

            ClientSideText txt1 = new ClientSideText()
            {
                Text = p.WikiContent
            };

            page.AddControl(txt1, -1);

            Microsoft.SharePoint.Client.ContentType newContentType = context.Web.GetContentTypeByName(targetContentTypeName);
            context.Load(newContentType);
            context.ExecuteQuery();

            ListItem item = page.PageListItem;

            context.Load(item);
            context.ExecuteQuery();

            item.Properties["ContentTypeId"] = newContentType.Id.StringValue;
            item["ContentTypeId"]            = newContentType.Id;

            item.Update();

            page.Save();


            context.ExecuteQuery();
        }
Пример #2
0
        public override int InsertItemByContentType <TContentType>(string listTitle, Fields <TContentType> fields)
        {
            Logger.Information("ClientQuery.InsertItemByContentType<TContentType>", string.Format("List = {0}, Items = {1}, ToArray = {2}", listTitle, string.Join(",", fields.ItemDictionary.Select(i => string.Format("{0} = {1}", i.Key, i.Value)).ToArray())));

            using (_clientContext)
            {
                var list = _clientContext.Web.Lists.GetByTitle(listTitle);

                var listCreationInformation = new ListItemCreationInformation();
                var listItem = list.AddItem(listCreationInformation);

                Microsoft.SharePoint.Client.ContentType oContentType = GetContentType(list,
                                                                                      typeof(TContentType).GetCustomAttribute <ContentAttribute>(true).Name);

                listItem["ContentTypeId"] = oContentType.Id.ToString();

                foreach (var field in fields.ItemDictionary)
                {
                    listItem[field.Key] = field.Value;
                }

                listItem.Update();

                _clientContext.ExecuteQuery();

                return(listItem.Id);
            }
        }
        public static string GetSettingsUrl(this SPClient.ContentType ct, TreeNode selectedNode)
        {
            // Link: <sitecollection|web>/_layouts/ManageContentType.aspx?ctype=0x0101009148F5A04DDD49CBA7127AADA5FB792B006973ACD696DC4858A76371B2FB2F439A
            // Link: <sitecollection|web>/_layouts/ManageContentType.aspx?List=%7BF798C4D9%2DEF29%2D4F8D%2DA1F1%2D4C70CFBAECE4%7D&ctype=0x010100A204AFB24228A94AB2D6195EB1705291

            if (selectedNode.Parent.Parent.Tag is SPClient.Site)
            {
                SPClient.Site site = selectedNode.Parent.Parent.Tag as SPClient.Site;
                return(string.Format("{0}/_layouts/ManageContentType.aspx?ctype={1}", site.RootWeb.GetUrl(), ct.Id));
            }
            else if (selectedNode.Parent.Parent.Tag is SPClient.Web)
            {
                // <sitecollection>/<web>/_layouts/15/ManageContentType.aspx?ctype=0x00A7470EADF4194E2E9ED1031B61DA088403000BE6CEFFF1ACA6429D14B2B7E0A03FE2
                // <sitecollection>/<web>/_layouts/15/start.aspx#/_layouts/15/ManageContentType.aspx?ctype=0x00A7470EADF4194E2E9ED1031B61DA088403000BE6CEFFF1ACA6429D14B2B7E0A03FE2&Source=https%3A%2F%2Fbramdejager%2Esharepoint%2Ecom%2Fsub%2F%5Flayouts%2F15%2Fmngctype%2Easpx

                SPClient.Web web = selectedNode.Parent.Parent.Tag as SPClient.Web;
                return(string.Format("{0}/_layouts/ManageContentType.aspx?ctype={1}", web.GetUrl(), ct.Id));
            }
            else if (selectedNode.Parent.Parent.Tag is SPClient.List)
            {
                SPClient.List list = selectedNode.Parent.Parent.Tag as SPClient.List;
                return(string.Format("{0}/_layouts/ManageContentType.aspx?list={1}&ctype={2}", list.ParentWeb.GetUrl(), list.Id, ct.Id));
            }
            else
            {
                return(string.Empty);
            }
        }
Пример #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();
        }
        public void Create(ClientContext ctx)
        {
            Console.WriteLine("Creating content type {0}", this.Name);
            var createdCT = ctx.Web.CreateContentType(this.Name, this.Description, this.ContentTypeId.ToString(), this.Group, this.Parent);

            this.AssociatedContentType = createdCT;

            foreach (var column in this.ColumnsToAdd)
            {
                createdCT.AddFieldByName(column.FieldInformation.InternalName);
            }
        }
Пример #6
0
        private static void CreatePages(ClientContext context, ErhvervManualProperies p, string targetContentTypeName)
        {
            var page = context.Web.AddClientSidePage(p.FileName, true);

            ClientSideText txt1 = new ClientSideText()
            {
                Text = p.WikiContent
            };

            page.AddControl(txt1, -1);

            Microsoft.SharePoint.Client.ContentType newContentType = context.Web.GetContentTypeByName(targetContentTypeName);
            context.Load(newContentType);
            context.ExecuteQuery();

            ListItem item = page.PageListItem;

            //item.Update();

            context.Load(item);
            context.ExecuteQuery();

            //item["ContentType"] = newContentType.Name;
            item.Properties["ContentTypeId"] = newContentType.Id.StringValue;
            item["ContentTypeId"]            = newContentType.Id;

            item.Update();
            //page.Save();

            //context.ExecuteQuery();



            //context.ExecuteQuery();

            if (!string.IsNullOrEmpty(p.ErhvervCategory))
            {
                SPOUtility.SetMetadataField(context, item, p.ErhvervCategory, "ErhvervCategory");
                item.Update();
            }
            if (!string.IsNullOrEmpty(p.ErhvervArea))
            {
                SPOUtility.SetMetadataField(context, item, p.ErhvervArea, "ErhvervArea");
                item.Update();
            }



            page.Save();


            context.ExecuteQuery();
        }
Пример #7
0
        private void button2_Click_1(object sender, EventArgs e)
        {
            Microsoft.SharePoint.Client.ContentType ctDoc = ctx.Web.ContentTypes.GetById("0x0101");
            ctx.Load(ctDoc);
            ctx.ExecuteQuery();

            ContentTypeCreationInformation ctci = new ContentTypeCreationInformation();
            ctci.Description = "test";
            ctci.Group = "Test";
            ctci.ParentContentType = ctDoc;
            ctci.Name = RandomString(10);

            ctx.Web.ContentTypes.Add(ctci);
            ctx.ExecuteQuery();
        }
Пример #8
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();
                }
            }
        }
        private void UpdateContentType(Web web, Microsoft.SharePoint.Client.ContentType existingContentType, PnPContentType templateContentType, TokenParser parser, bool isNoScriptSite = false)
        {
            var isDirty = false;

            if (existingContentType.Hidden != templateContentType.Hidden)
            {
                // scope.LogPropertyUpdate("Hidden");
                existingContentType.Hidden = templateContentType.Hidden;
                isDirty = true;
            }
            if (existingContentType.ReadOnly != templateContentType.ReadOnly)
            {
                //scope.LogPropertyUpdate("ReadOnly");
                existingContentType.ReadOnly = templateContentType.ReadOnly;
                isDirty = true;
            }
            if (existingContentType.Sealed != templateContentType.Sealed)
            {
                //scope.LogPropertyUpdate("Sealed");
                existingContentType.Sealed = templateContentType.Sealed;
                isDirty = true;
            }
            if (templateContentType.Description != null && existingContentType.Description != parser.ParseString(templateContentType.Description))
            {
                //scope.LogPropertyUpdate("Description");
                existingContentType.Description = parser.ParseString(templateContentType.Description);
                isDirty = true;
            }
            if (templateContentType.DocumentTemplate != null && existingContentType.DocumentTemplate != parser.ParseString(templateContentType.DocumentTemplate))
            {
                //scope.LogPropertyUpdate("DocumentTemplate");
                existingContentType.DocumentTemplate = parser.ParseString(templateContentType.DocumentTemplate);
                isDirty = true;
            }
            if (existingContentType.Name != parser.ParseString(templateContentType.Name))
            {
                //scope.LogPropertyUpdate("Name");
                existingContentType.Name = parser.ParseString(templateContentType.Name);
                isDirty = true;
                // CT is being renamed, add an extra token to the tokenparser
                parser.AddToken(new ContentTypeIdToken(web, existingContentType.Name, existingContentType.StringId));
            }
            if (templateContentType.Group != null && existingContentType.Group != parser.ParseString(templateContentType.Group))
            {
                // scope.LogPropertyUpdate("Group");
                existingContentType.Group = parser.ParseString(templateContentType.Group);
                isDirty = true;
            }
            if (!isNoScriptSite)
            {
                if (templateContentType.DisplayFormUrl != null && existingContentType.DisplayFormUrl != parser.ParseString(templateContentType.DisplayFormUrl))
                {
                    // scope.LogPropertyUpdate("DisplayFormUrl");
                    existingContentType.DisplayFormUrl = parser.ParseString(templateContentType.DisplayFormUrl);
                    isDirty = true;
                }
                if (templateContentType.EditFormUrl != null && existingContentType.EditFormUrl != parser.ParseString(templateContentType.EditFormUrl))
                {
                    //scope.LogPropertyUpdate("EditFormUrl");
                    existingContentType.EditFormUrl = parser.ParseString(templateContentType.EditFormUrl);
                    isDirty = true;
                }
                if (templateContentType.NewFormUrl != null && existingContentType.NewFormUrl != parser.ParseString(templateContentType.NewFormUrl))
                {
                    // scope.LogPropertyUpdate("NewFormUrl");
                    existingContentType.NewFormUrl = parser.ParseString(templateContentType.NewFormUrl);
                    isDirty = true;
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(parser.ParseString(templateContentType.DisplayFormUrl)) ||
                    !String.IsNullOrEmpty(parser.ParseString(templateContentType.EditFormUrl)) ||
                    !String.IsNullOrEmpty(parser.ParseString(templateContentType.NewFormUrl)))
                {
                    // log message
                    Debug.WriteLine("Skip custom from urls {0}", existingContentType.Name);
                    //scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ContentTypes_SkipCustomFormUrls, existingContentType.Name);
                }
            }

#if !SP2013
            if (templateContentType.Name.ContainsResourceToken())
            {
                existingContentType.NameResource.SetUserResourceValue(templateContentType.Name, parser);
                isDirty = true;
            }
            if (templateContentType.Description.ContainsResourceToken())
            {
                existingContentType.DescriptionResource.SetUserResourceValue(templateContentType.Description, parser);
                isDirty = true;
            }
#endif
            if (isDirty)
            {
                existingContentType.Update(true);
                web.Context.ExecuteQueryRetry();
            }
            // Delta handling
            existingContentType.EnsureProperty(c => c.FieldLinks);
            List <Guid> targetIds = existingContentType.FieldLinks.AsEnumerable().Select(c1 => c1.Id).ToList();
            List <Guid> sourceIds = templateContentType.FieldRefs.Select(c1 => c1.Id).ToList();

            var fieldsNotPresentInTarget = sourceIds.Except(targetIds).ToArray();

            if (fieldsNotPresentInTarget.Any())
            {
                foreach (var fieldId in fieldsNotPresentInTarget)
                {
                    var fieldRef = templateContentType.FieldRefs.Find(fr => fr.Id == fieldId);
                    var field    = web.Fields.GetById(fieldId);
                    //scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ContentTypes_Adding_field__0__to_content_type, fieldId);
                    web.AddFieldToContentType(existingContentType, field, fieldRef.Required, fieldRef.Hidden);
                }
            }

            isDirty = false;
            foreach (var fieldId in targetIds.Intersect(sourceIds))
            {
                var fieldLink = existingContentType.FieldLinks.FirstOrDefault(fl => fl.Id == fieldId);
                var fieldRef  = templateContentType.FieldRefs.Find(fr => fr.Id == fieldId);
                if (fieldRef != null)
                {
                    //scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_ContentTypes_Field__0__exists_in_content_type, fieldId);
                    if (fieldLink.Required != fieldRef.Required)
                    {
                        //scope.LogPropertyUpdate("Required");
                        fieldLink.Required = fieldRef.Required;
                        isDirty            = true;
                    }
                    if (fieldLink.Hidden != fieldRef.Hidden)
                    {
                        //scope.LogPropertyUpdate("Hidden");
                        fieldLink.Hidden = fieldRef.Hidden;
                        isDirty          = true;
                    }
                }
            }

            // The new CT is a DocumentSet, and the target should be, as well
            if (templateContentType.DocumentSetTemplate != null)
            {
                if (!Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate.IsChildOfDocumentSetContentType(web.Context, existingContentType).Value)
                {
                    //scope.LogError(CoreResources.Provisioning_ObjectHandlers_ContentTypes_InvalidDocumentSet_Update_Request, existingContentType.Id, existingContentType.Name);
                }
                else
                {
                    Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate templateToUpdate =
                        Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate.GetDocumentSetTemplate(web.Context, existingContentType);

                    // TODO: Implement Delta Handling
                    //scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ContentTypes_DocumentSet_DeltaHandling_OnHold, existingContentType.Id, existingContentType.Name);
                }
            }

            if (isDirty)
            {
                existingContentType.Update(true);
                web.Context.ExecuteQueryRetry();
            }
        }
        private Microsoft.SharePoint.Client.ContentType CreateContentType(Web web, PnPContentType templateContentType, TokenParser parser, FileConnectorBase connector,
                                                                          List <Microsoft.SharePoint.Client.ContentType> existingCTs = null, List <Microsoft.SharePoint.Client.Field> existingFields = null, bool isNoScriptSite = false)
        {
            var name        = parser.ParseString(templateContentType.Name);
            var description = parser.ParseString(templateContentType.Description);
            var id          = parser.ParseString(templateContentType.Id);
            var group       = parser.ParseString(templateContentType.Group);

            var createdCT = web.CreateContentType(name, description, id, group);

            foreach (var fieldRef in templateContentType.FieldRefs)
            {
                var field = web.Fields.FirstOrDefault(f => f.Id == fieldRef.Id);
                //var field = web.Fields.GetById(fieldRef.Id);
                if (field != null)
                {
                    web.AddFieldToContentType(createdCT, field, fieldRef.Required, fieldRef.Hidden);
                }
            }

            // Add new CTs
            parser.AddToken(new ContentTypeIdToken(web, name, id));

#if !ONPREMISES
            // Set resources
            if (templateContentType.Name.ContainsResourceToken())
            {
                createdCT.NameResource.SetUserResourceValue(templateContentType.Name, parser);
            }
            if (templateContentType.Description.ContainsResourceToken())
            {
                createdCT.DescriptionResource.SetUserResourceValue(templateContentType.Description, parser);
            }
#endif
            //Reorder the elements so that the new created Content Type has the same order as defined in the
            //template. The order can be different if the new Content Type inherits from another Content Type.
            //In this case the new Content Type has all field of the original Content Type and missing fields
            //will be added at the end. To fix this issue we ordering the fields once more.

            createdCT.FieldLinks.Reorder(templateContentType.FieldRefs.Select(fld => parser.ParseString(fld.Name)).ToArray());

            createdCT.ReadOnly = templateContentType.ReadOnly;
            createdCT.Hidden   = templateContentType.Hidden;
            createdCT.Sealed   = templateContentType.Sealed;

            if (templateContentType.DocumentSetTemplate == null)
            {
                // Only apply a document template when the contenttype is not a document set
                if (!string.IsNullOrEmpty(parser.ParseString(templateContentType.DocumentTemplate)))
                {
                    createdCT.DocumentTemplate = parser.ParseString(templateContentType.DocumentTemplate);
                }
            }

            // Skipping updates of forms as we can't upload forms to noscript sites
            if (!isNoScriptSite)
            {
                if (!String.IsNullOrEmpty(parser.ParseString(templateContentType.NewFormUrl)))
                {
                    createdCT.NewFormUrl = parser.ParseString(templateContentType.NewFormUrl);
                }
                if (!String.IsNullOrEmpty(parser.ParseString(templateContentType.EditFormUrl)))
                {
                    createdCT.EditFormUrl = parser.ParseString(templateContentType.EditFormUrl);
                }
                if (!String.IsNullOrEmpty(parser.ParseString(templateContentType.DisplayFormUrl)))
                {
                    createdCT.DisplayFormUrl = parser.ParseString(templateContentType.DisplayFormUrl);
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(parser.ParseString(templateContentType.DisplayFormUrl)) ||
                    !String.IsNullOrEmpty(parser.ParseString(templateContentType.EditFormUrl)) ||
                    !String.IsNullOrEmpty(parser.ParseString(templateContentType.NewFormUrl)))
                {
                    // log message
                    //scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ContentTypes_SkipCustomFormUrls, name);
                }
            }

            createdCT.Update(true);
            web.Context.ExecuteQueryRetry();

            // If the CT is a DocumentSet
            if (templateContentType.DocumentSetTemplate != null)
            {
                // Retrieve a reference to the DocumentSet Content Type
                Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate documentSetTemplate =
                    Microsoft.SharePoint.Client.DocumentSet.DocumentSetTemplate.GetDocumentSetTemplate(web.Context, createdCT);

                // Load the collections to allow for deletion scenarions
                web.Context.Load(documentSetTemplate, d => d.AllowedContentTypes, d => d.DefaultDocuments, d => d.SharedFields, d => d.WelcomePageFields);
                web.Context.ExecuteQueryRetry();

                if (!String.IsNullOrEmpty(templateContentType.DocumentSetTemplate.WelcomePage))
                {
                    // TODO: Customize the WelcomePage of the DocumentSet
                }

                // Add additional content types to the set of allowed content types
                bool hasDefaultDocumentContentTypeInTemplate = false;
                foreach (String ctId in templateContentType.DocumentSetTemplate.AllowedContentTypes)
                {
                    Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == ctId);
                    if (ct != null)
                    {
                        if (ct.Id.StringValue.Equals("0x0101", StringComparison.InvariantCultureIgnoreCase))
                        {
                            hasDefaultDocumentContentTypeInTemplate = true;
                        }

                        documentSetTemplate.AllowedContentTypes.Add(ct.Id);
                    }
                }
                // If the default document content type (0x0101) is not in our definition then remove it
                if (!hasDefaultDocumentContentTypeInTemplate)
                {
                    Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == "0x0101");
                    if (ct != null)
                    {
                        documentSetTemplate.AllowedContentTypes.Remove(ct.Id);
                    }
                }

                if (!isNoScriptSite)
                {
                    foreach (var doc in templateContentType.DocumentSetTemplate.DefaultDocuments)
                    {
                        Microsoft.SharePoint.Client.ContentType ct = existingCTs.FirstOrDefault(c => c.StringId == doc.ContentTypeId);
                        if (ct != null)
                        {
                            using (Stream fileStream = connector.GetFileStream(doc.FileSourcePath))
                            {
                                documentSetTemplate.DefaultDocuments.Add(doc.Name, ct.Id, ReadFullStream(fileStream));
                            }
                        }
                    }
                }
                else
                {
                    if (templateContentType.DocumentSetTemplate.DefaultDocuments.Any())
                    {
                        //scope.LogWarning(CoreResources.Provisioning_ObjectHandlers_ContentTypes_SkipDocumentSetDefaultDocuments, name);
                    }
                }

                foreach (var sharedField in templateContentType.DocumentSetTemplate.SharedFields)
                {
                    Microsoft.SharePoint.Client.Field field = existingFields.FirstOrDefault(f => f.Id == sharedField);
                    if (field != null)
                    {
                        documentSetTemplate.SharedFields.Add(field);
                    }
                }

                foreach (var welcomePageField in templateContentType.DocumentSetTemplate.WelcomePageFields)
                {
                    Microsoft.SharePoint.Client.Field field = existingFields.FirstOrDefault(f => f.Id == welcomePageField);
                    if (field != null)
                    {
                        documentSetTemplate.WelcomePageFields.Add(field);
                    }
                }

                documentSetTemplate.Update(true);
                web.Context.ExecuteQueryRetry();
            }

            web.Context.Load(createdCT);
            web.Context.ExecuteQueryRetry();

            return(createdCT);
        }
Пример #11
0
        /// <summary>
        /// Adds the parent
        /// </summary>
        /// <param name="sourceContentTypeCollection">ContentTypeCollection of the source SharePoint</param>
        /// <param name="targetContentTypeCollection">ContentTypeCollection of the target SharePoint</param>
        /// <param name="parentContentType">ContentType of parent</param>
        /// <returns>return parent ContentType or null, if there is no parent</returns>
        private Microsoft.SharePoint.Client.ContentType AddParent(ContentTypeCollection sourceContentTypeCollection, ContentTypeCollection targetContentTypeCollection, Microsoft.SharePoint.Client.ContentType parentContentType)
        {
            Console.WriteLine("add parent ContentType...");
            Logger.AddMessage("adding parent ContentType...");
            if (parentContentType == null || parentContentType.Name == null)
            {
                return(null);
            }

            try
            {
                return(this.GetContentTypeByName(targetContentTypeCollection, parentContentType.Name));
            }
            catch (ElementsMigrationException e)
            {
                Console.WriteLine("have to create parent content type. " + e.Message);
                Logger.AddMessage("have to create parent ContentType");
            }

            this.CreateContentType(sourceContentTypeCollection, targetContentTypeCollection, parentContentType);

            return(this.GetContentTypeByName(targetContentTypeCollection, parentContentType.Name));
        }
Пример #12
0
        /// <summary>
        /// Creates a new ContentType.
        /// </summary>
        /// <param name="sourceContentTypeCollection">ContentTypeCollection of the source SharePoint</param>
        /// <param name="targetContentTypeCollection">ContentTypeCollection of the target SharePoint</param>
        /// <param name="sourceContentType">ContentType from source with information for the new one</param>
        private void CreateContentType(ContentTypeCollection sourceContentTypeCollection, ContentTypeCollection targetContentTypeCollection, Microsoft.SharePoint.Client.ContentType sourceContentType)
        {
            ContentTypeCreationInformation creationObject = new ContentTypeCreationInformation();

            creationObject.Description = sourceContentType.Description;
            creationObject.Group       = sourceContentType.Group;
            creationObject.Name        = sourceContentType.Name;
            ////  creationObject.ParentContentType = this.AddParent(sourceContentTypeCollection, targetContentTypeCollection, sourceContentType.Parent);

            ContentType targetContentType = targetContentTypeCollection.Add(creationObject);

            try
            {
                targetContentType.DisplayFormTemplateName = sourceContentType.DisplayFormTemplateName;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.DisplayFormUrl = sourceContentType.DisplayFormUrl;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.DocumentTemplate = sourceContentType.DocumentTemplate;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.EditFormTemplateName = sourceContentType.EditFormTemplateName;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.EditFormUrl = sourceContentType.EditFormUrl;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.Hidden = sourceContentType.Hidden;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.NewFormTemplateName = sourceContentType.NewFormTemplateName;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.NewFormUrl = sourceContentType.NewFormUrl;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.ReadOnly = sourceContentType.ReadOnly;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.Sealed = sourceContentType.Sealed;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }

            try
            {
                targetContentType.Tag = sourceContentType.Tag;
            }
            catch (PropertyOrFieldNotInitializedException)
            {
            }
        }