简体   繁体   中英

SPMeta2 System.InvalidCastException while trying to deploy model

I am currently trying to create a simple list in Sharepoint 365, using SPMeta2 framework. 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. It says it needs SiteModelHost, but when I give it SiteModelHost I get same exception, saying 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.

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 model should be deployed within 'web model host'.

With CSOM provision, SiteModelHost.FromClientContext(context) and WebModelHost.FromClientContext(context) should be used to push site or web model accordingly.

Here is a working code for site model. We removed lists (they belong to web) and use 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.

        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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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