简体   繁体   English

尝试部署模型时SPMeta2 System.InvalidCastException

[英]SPMeta2 System.InvalidCastException while trying to deploy model

I am currently trying to create a simple list in Sharepoint 365, using SPMeta2 framework. 我目前正在尝试使用SPMeta2框架在Sharepoint 365中创建一个简单的列表。 I have tried doing it according to the documentation, and got an exception, then I've decided to simply copy code from their samples, located here Now, when I'm trying to deploy my model I keep getting System.InvalidCastException. 我已经尝试按照文档进行操作,并得到一个例外,然后我决定只是从他们的示例中复制代码,位于此处现在,当我尝试部署我的模型时,我不断收到System.InvalidCastException。 It says it needs SiteModelHost, but when I give it SiteModelHost I get same exception, saying WebModelHost. 它说它需要SiteModelHost,但当我给它SiteModelHost我得到相同的异常,说WebModelHost。 Obviously, when I give it web it asks for site. 显然,当我给它网站它要求网站。 I would be grateful for any pointers. 我会感激任何指针。

Here is my code for deploying the model: 这是我部署模型的代码:

class Program
{
    static void Main (string[] args)
    {
        var targetSite = new Uri("https://url.com/");
        var login = "***.com";
        var password = "1234";
        var securePassword = new SecureString();
        foreach (char c in password)
        {
            securePassword.AppendChar(c);
        }

        var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
        var model = SPMeta2Model
                    .NewSiteModel(site =>
                    {
                        site
                            .WithFields(fields =>
                            {
                                fields
                                .AddField(FieldModels.Contact)
                                .AddField(FieldModels.Details);
                            })
                            .WithContentTypes(contentTypes =>
                            {
                                contentTypes
                                .AddContentType(ContentTypeModels.CustomItem)
                                .AddContentType(ContentTypeModels.CustomDocument);
                            })
                            .WithLists(lists =>
                            {
                                lists
                                .AddList(ListModels.TestLibrary)
                                .AddList(ListModels.TestList)
                                .AddList(ListModels.TestLinksList);
                            });
                    });
        using (var context = new ClientContext(targetSite))
        {
            context.Credentials = onlineCredentials;


            var povisionService = new CSOMProvisionService();

            povisionService.DeployModel(WebModelHost.FromClientContext(context), model); // WebModelHost/SiteModelHost - same exception
        }

Easy. 简单。

SPMeta2 allows to several types of models. SPMeta2允许几种类型的模型。

Site model reflects and contains all artefacts which could be deployed at site level - site features, custom actions, site fields, content types and so on. 站点模型反映并包含可在站点级别部署的所有人工制品 - 站点功能,自定义操作,站点字段,内容类型等。 Site model should be deployed within 'site model host'. 站点模型应该部署在“站点模型主机”中。

Web model reflects and contains all artefacts which could be deployed at web level - web features, lists, list views and so on. Web模型反映并包含可以在Web级别部署的所有工件 - Web功能,列表,列表视图等。 Web model should be deployed within 'web model host'. Web模型应该部署在“Web模型主机”中。

With CSOM provision, SiteModelHost.FromClientContext(context) and WebModelHost.FromClientContext(context) should be used to push site or web model accordingly. 使用CSOM提供时,应使用SiteModelHost.FromClientContext(context)和WebModelHost.FromClientContext(context)来相应地推送站点或Web模型。

Here is a working code for site model. 这是站点模型的工作代码。 We removed lists (they belong to web) and use SiteModelHost. 我们删除了列表(它们属于Web)并使用SiteModelHost。

            var siteModel = SPMeta2Model
                   .NewSiteModel(site =>
                   {
                       site
                           .WithFields(fields =>
                           {
                               fields
                               .AddField(FieldModels.Contact)
                               .AddField(FieldModels.Details);
                           })
                           .WithContentTypes(contentTypes =>
                           {
                               contentTypes
                               .AddContentType(ContentTypeModels.CustomItem)
                               .AddContentType(ContentTypeModels.CustomDocument);
                           });
                   });

            using (var context = new ClientContext(targetSite))
            {
                var povisionService = new CSOMProvisionService();
                povisionService.DeployModel(SiteModelHost.FromClientContext(context), siteModel);
            }

And the web model here. 这里的网络模型。 We removed fields/content types, and use only lists plus WebModelHost. 我们删除了字段/内容类型,并仅使用列表和WebModelHost。

        var webModel = SPMeta2Model
               .NewWebModel(web =>
               {
                   web
                       .WithLists(lists =>
                       {
                           lists
                               .AddList(ListModels.TestLibrary)
                               .AddList(ListModels.TestList)
                               .AddList(ListModels.TestLinksList);
                       });
               });

        using (var context = new ClientContext(targetSite))
        {
            var povisionService = new CSOMProvisionService();
            povisionService.DeployModel(WebModelHost.FromClientContext(context), webModel);
        }

Finally, here are several links to get started: 最后,这里有几个入门链接:

Let me know how it goes, and if you need further assistance. 让我知道它是怎么回事,如果你需要进一步的帮助。 Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM