Пример #1
0
        protected virtual ContentType GetContentType(CSOMModelHostBase modelHost, DocumentSetDefinition definition)
        {
            var contentTypeName = definition.ContentTypeName;
            var contentTypeId   = definition.ContentTypeId;

            var context = modelHost.HostClientContext;
            var web     = modelHost.HostWeb;

            ContentType contentType = null;

#if !NET35
            if (!string.IsNullOrEmpty(contentTypeId))
            {
                var tmpContentType = context.LoadQuery(web.AvailableContentTypes.Where(ct => ct.StringId == contentTypeId));
                context.ExecuteQueryWithTrace();

                contentType = tmpContentType.FirstOrDefault();

                if (contentType == null)
                {
                    throw new SPMeta2Exception(string.Format("Cannot find content type by ID:[{0}]", contentTypeId));
                }
            }
            else if (!string.IsNullOrEmpty(contentTypeName))
            {
                var tmpContentType = context.LoadQuery(web.AvailableContentTypes.Where(ct => ct.Name == contentTypeName));
                context.ExecuteQueryWithTrace();

                contentType = tmpContentType.FirstOrDefault();

                if (contentType == null)
                {
                    throw new SPMeta2Exception(string.Format("Cannot find content type by Name:[{0}]", contentTypeName));
                }
            }
#endif

#if NET35
            // SP2010 CSOM does not have an option to get the content type by ID
            // fallback to Name, and that's a huge thing all over the M2 library and provision

            var tmpContentType = context.LoadQuery(web.AvailableContentTypes.Where(ct => ct.Name == contentTypeName));
            context.ExecuteQueryWithTrace();

            contentType = tmpContentType.FirstOrDefault();

            if (contentType == null)
            {
                throw new SPMeta2Exception(string.Format("Cannot find content type by Name:[{0}]", contentTypeName));
            }
#endif

            return(contentType);
        }
Пример #2
0
        protected virtual string ResolveTokenizedUrl(CSOMModelHostBase context, string urlValue)
        {
            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Original Url: [{0}]", urlValue);

            var newUrlValue = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
            {
                Value   = urlValue,
                Context = context
            }).Value;

            TraceService.VerboseFormat((int)LogEventId.ModelProvisionCoreCall, "Token replaced Url: [{0}]", newUrlValue);

            return(newUrlValue);
        }
Пример #3
0
        private void DeployArtifact(CSOMModelHostBase modelHost, Folder folder, DocumentSetDefinition definition)
        {
            var currentDocumentSet = GetExistingDocumentSet(modelHost, folder, definition);

            var context = modelHost.HostClientContext;
            var web     = modelHost.HostWeb;

            var documentSetName = definition.Name;

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = currentDocumentSet,
                ObjectType       = typeof(Folder),
                ObjectDefinition = definition,
                ModelHost        = modelHost
            });

            if (currentDocumentSet == null)
            {
                var contentType = GetContentType(modelHost, definition);

                DocumentSet.Create(context, folder, documentSetName, contentType.Id);
                context.ExecuteQueryWithTrace();

                currentDocumentSet = GetExistingDocumentSet(modelHost, folder, definition);
            }

            if (!string.IsNullOrEmpty(definition.Description))
            {
                currentDocumentSet.ListItemAllFields["DocumentSetDescription"] = definition.Description;
                currentDocumentSet.ListItemAllFields.Update();

                context.ExecuteQueryWithTrace();
            }

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = currentDocumentSet,
                ObjectType       = typeof(Folder),
                ObjectDefinition = definition,
                ModelHost        = modelHost
            });
        }
Пример #4
0
        private void InternalSetCurrentSearchConfig(
            CSOMModelHostBase modelHost,
            Web web,
            SearchSettingsConfig searchSettings, bool isWebLevel)
        {
            var propertyBagName = "SRCH_SB_SET_SITE";

            if (isWebLevel)
            {
                propertyBagName = "SRCH_SB_SET_WEB";
            }

            var serializer = new JavaScriptSerializer();

            var props = web.AllProperties;

            props[propertyBagName] = serializer.Serialize(searchSettings);
        }
Пример #5
0
        protected void InternalSetSearchCenterUrl(CSOMModelHostBase modelHost, Web web, string url, bool isWebLevel)
        {
            var propertyBagName = "SRCH_ENH_FTR_URL_SITE";

            if (isWebLevel)
            {
                propertyBagName = "SRCH_ENH_FTR_URL_WEB";
            }

            if (!string.IsNullOrEmpty(url))
            {
                url = TokenReplacementService.ReplaceTokens(new TokenReplacementContext
                {
                    Context = modelHost,
                    Value   = url
                }).Value;

                var props = web.AllProperties;
                props[propertyBagName] = url;
            }
        }
Пример #6
0
        protected virtual Folder GetExistingDocumentSet(CSOMModelHostBase modelHost, Folder folder, DocumentSetDefinition definition)
        {
            var folderName   = definition.Name;
            var parentFolder = folder;

            var context = parentFolder.Context;

            context.Load(parentFolder, f => f.Folders);
            context.ExecuteQueryWithTrace();

            // dirty stuff, needs to be rewritten
            var currentFolder = parentFolder
                                .Folders
                                .OfType <Folder>()
                                .FirstOrDefault(f => f.Name == folderName);

            if (currentFolder != null)
            {
                return(currentFolder);
            }

            return(null);
        }
Пример #7
0
 protected virtual void SetSearchCenterUrlAtSiteLevel(CSOMModelHostBase modelHost, Web web, string url)
 {
     InternalSetSearchCenterUrl(modelHost, web, url, false);
 }
Пример #8
0
 protected virtual void SetCurrentSearchConfigAtSiteLevel(CSOMModelHostBase modelHost, Web web, SearchSettingsConfig searchSettings)
 {
     InternalSetCurrentSearchConfig(modelHost, web, searchSettings, false);
 }
Пример #9
0
 protected virtual string ResolveTokenizedUrl(CSOMModelHostBase context, NavigationNodeDefinitionBase rootNode)
 {
     return(ResolveTokenizedUrl(context, rootNode.Url));
 }
Пример #10
0
        public override void DeployModel(object modelHost, DefinitionBase model)
        {
            Guid?OldWebParKey = null;

            var listItemModelHost = modelHost.WithAssertAndCast <ListItemModelHost>("modelHost", value => value.RequireNotNull());
            var webPartModel      = model.WithAssertAndCast <WebPartDefinitionBase>("model", value => value.RequireNotNull());

            try
            {
                OnBeforeDeploy(listItemModelHost, webPartModel);

                CurrentModelHost     = modelHost.WithAssertAndCast <CSOMModelHostBase>("modelHost", value => value.RequireNotNull());
                CurrentClientContext = listItemModelHost.HostClientContext;

                var context         = listItemModelHost.HostClientContext;
                var currentPageFile = GetCurrentPageFile(listItemModelHost);


                if (listItemModelHost.HostFolder != null)
                {
                    // TODO, re-implement for SP2010 CSOM
                    // the following stuff is needed only for the web part deployment to the non-web part pages
                    // like, view/upload/details pages in the lost/libs
                    // hope no one would use that case on 2010 - folks, migrate to 2013 at least! :)
#if !NET35
                    if (!listItemModelHost.HostFolder.IsPropertyAvailable("Properties") ||
                        listItemModelHost.HostFolder.Properties.FieldValues.Count == 0)
                    {
                        listItemModelHost.HostFolder.Context.Load(listItemModelHost.HostFolder, f => f.Properties);
                        //folder.Context.Load(folder, f => f.Properties);

                        listItemModelHost.HostFolder.Context.ExecuteQueryWithTrace();
                    }
#endif
                }

#if !NET35
                var doesFileHasListItem =
                    //Forms folders
                    !(listItemModelHost.HostFolder != null
                      &&
                      (listItemModelHost.HostFolder.Properties.FieldValues.ContainsKey("vti_winfileattribs")
                       &&
                       listItemModelHost.HostFolder.Properties.FieldValues["vti_winfileattribs"].ToString() ==
                       "00000012"));
#endif

#if NET35
                // TODO, re-implement for SP2010 CSOM
                // the following stuff is needed only for the web part deployment to the non-web part pages

                var doesFileHasListItem = true;
#endif


                ModuleFileModelHandler.WithSafeFileOperation(listItemModelHost.HostList,
                                                             currentPageFile, pageFile =>
                {
                    Guid?webPartStoreKey = null;

                    InternalOnBeforeWebPartProvision(new WebPartProcessingContext
                    {
                        ListItemModelHost = listItemModelHost,
                        WebPartDefinition = webPartModel,
                        WebPartStoreKey   = webPartStoreKey
                    });

                    //var fileContext = pageFile.Context;
                    ListItem fileListItem = null;

                    if (webPartModel.AddToPageContent)
                    {
                        // pre load here to be used later

                        var fileContext = pageFile.Context;

                        fileListItem = pageFile.ListItemAllFields;

                        fileContext.Load(fileListItem);
                        fileContext.ExecuteQueryWithTrace();
                    }

                    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 wpDefinition;

                    WebPart existingWebPart = null;

                    // TODO
                    var tmpWp = FindExistingWebPart(webPartDefenitions, webPartModel, out wpDefinition);

                    InvokeOnModelEvent(this, new ModelEventArgs
                    {
                        CurrentModelNode = null,
                        Model            = null,
                        EventType        = ModelEventType.OnProvisioning,
                        Object           = existingWebPart,
                        ObjectType       = typeof(WebPart),
                        ObjectDefinition = webPartModel,
                        ModelHost        = modelHost
                    });

                    if (wpDefinition != null)
                    {
                        OldWebParKey = wpDefinition.Id;

                        TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject,
                                                 "Deleting current web part.");

                        wpDefinition.DeleteWebPart();
                        wpDefinition.Context.ExecuteQueryWithTrace();
                    }
                    else
                    {
                        existingWebPart = tmpWp;
                    }

                    Microsoft.SharePoint.Client.WebParts.WebPartDefinition webPartAddedDefinition = null;

                    if (existingWebPart == null)
                    {
                        TraceService.Information((int)LogEventId.ModelProvisionProcessingNewObject,
                                                 "Processing new web part");

                        var webPartXML = GetWebpartXmlDefinition(listItemModelHost, webPartModel);
                        webPartXML     = ProcessCommonWebpartProperties(webPartXML, webPartModel);

                        //// handle wiki page
                        //if (webPartModel.AddToPageContent)
                        //{

                        //    HandleWikiPageProvision(fileListItem, webPartModel);
                        //}

                        var webPartDefinition  = webPartManager.ImportWebPart(webPartXML);
                        webPartAddedDefinition = webPartManager.AddWebPart(webPartDefinition.WebPart,
                                                                           webPartModel.ZoneId,
                                                                           webPartModel.ZoneIndex);

                        context.Load(webPartAddedDefinition);
                        context.ExecuteQueryWithTrace();

                        if (webPartAddedDefinition != null && webPartAddedDefinition.ServerObjectIsNull == false)
                        {
                            existingWebPart = webPartAddedDefinition.WebPart;
                            webPartStoreKey = webPartAddedDefinition.Id;
                        }

                        // handle wiki page
                        if (webPartModel.AddToPageContent)
                        {
                            HandleWikiPageProvision(fileListItem, webPartModel, webPartStoreKey, OldWebParKey);
                        }

                        existingWebPart = webPartDefinition.WebPart;

                        InvokeOnModelEvent(this, new ModelEventArgs
                        {
                            CurrentModelNode = null,
                            Model            = null,
                            EventType        = ModelEventType.OnProvisioned,
                            Object           = existingWebPart,
                            ObjectType       = typeof(WebPart),
                            ObjectDefinition = webPartModel,
                            ModelHost        = modelHost
                        });
                    }
                    else
                    {
                        TraceService.Information((int)LogEventId.ModelProvisionProcessingExistingObject,
                                                 "Processing existing web part");

                        if (webPartModel.AddToPageContent)
                        {
                            //HandleWikiPageProvision(fileListItem, webPartModel);
                        }

                        InvokeOnModelEvent(this, new ModelEventArgs
                        {
                            CurrentModelNode = null,
                            Model            = null,
                            EventType        = ModelEventType.OnProvisioned,
                            Object           = existingWebPart,
                            ObjectType       = typeof(WebPart),
                            ObjectDefinition = webPartModel,
                            ModelHost        = modelHost
                        });
                    }

                    context.ExecuteQueryWithTrace();

                    if (webPartAddedDefinition != null && webPartAddedDefinition.ServerObjectIsNull == false)
                    {
                        existingWebPart = webPartAddedDefinition.WebPart;
                        webPartStoreKey = webPartAddedDefinition.Id;
                    }

                    InternalOnAfterWebPartProvision(new WebPartProcessingContext
                    {
                        ListItemModelHost = listItemModelHost,
                        WebPartDefinition = webPartModel,
                        WebPartStoreKey   = webPartStoreKey
                    });

                    return(pageFile);
                }, doesFileHasListItem);
            }
            finally
            {
                OnAfterDeploy(listItemModelHost, webPartModel);
            }
        }