通过 CSOM / JSOM解决方案 上传 和 激活

Uploading and activating sandbox solutions via CSOM/JSOM

In this post we’re discussing how to upload and activate a sandbox solution via CSOM/JSOM. First, we’ll do it using C#. That’s a little bit easier and we’ve seen several examples discussing that (such as this one:http://blogs.msdn.com/b/frank_marasco/archive/2014/08/10/upload-and-activate-sandbox-solutions-using-csom.aspx , probably the first one written about this topic and without it, we couldn’t have written this post). Then, we’ll do it using JSOM which has some additional challenges. We didn’t find any resources discussing that, so there should be value in that.

First off, create a new sub site using the template team site. Then, save that sub site via Site Settings > Save site as template and go to the Solution Gallery and download it somewhere on your local file system. That way, you’ve created a site template that can be used to upload and activate later on. When finished, remove that solution from the Solution Gallery (otherwise, there’s not much point in uploading it again programmatically, now is there?).

In the C# version, we’re basically doing this:

  1. Establish the correct client context
  2. Upload the solution directly to the Solution gallery.
  3. Read the site template (*.wsp) from the local file system.
  4. Create a new FileCreationInformation object representing the site template that will be uploaded.
  5. Upload the solution somewhere. Please note: somewhere can be the solution gallery, but it can also be any other document library. This is because the DesignPackageInfo class accepts a relative URL of an asset located in SharePoint, takes it, and installs it in the Solution Gallery.
  6. Use the DesignPackageInfo class to install and activate the sandbox solution.

The code to do that looks like this:

using Microsoft.SharePoint.Client;

using Microsoft.SharePoint.Client.Publishing;

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Web;

namespace SolutionUploader

{

    class Program

    {
    
        static void Main(string[] args)
        
        {
    
            try
    
            {
            
                ClientContext context = new ClientContext(“http://%5Burl of site collection] “);
                
                Web web = context.Web;
                
                context.Load(web);
                
                var lib = web.Lists.GetByTitle(“[destination lib]”);
                
                context.Load(lib);
                
                var filePath = @”D:\TestTemplateFolder\TestTemplate.wsp”;
                
                var file = new FileStream(filePath, FileMode.Open);
                
                var fileCI = new FileCreationInformation()
                
                {
                
                    ContentStream = file,
                    
                    Url = “TestSiteTemplate-v1.2.wsp”,
                    
                    Overwrite = true
                
                };
                
                var uploadedFile = lib.RootFolder.Files.Add(fileCI);
                
                context.Load(uploadedFile);
                
                context.ExecuteQuery();
                
                var wsp = new DesignPackageInfo()
                
                {
                
                    // Guid can be empty and is autofilled,
                    
                    // but specifying it explicitly makes it easier if you want to remove it
                    
                    // later.
                    
                    PackageGuid = new Guid(“[GUID]”),
                    
                    //PackageGuid = Guid.Empty,
                    
                    MajorVersion = 1,
                    
                    MinorVersion = 0,
                    
                    PackageName = “[package name]”
                
                };
                
                string filerelativeurl = “[site relative path of *.wsp] “;
                
                DesignPackage.Install(context, context.Site, wsp, filerelativeurl);
                
                context.ExecuteQuery();
                
            }
            
            catch (Exception ex)
            
            {
            
                 Console.WriteLine(ex.Message);
            
            }
        
        }
    
    }

}

You may notice that after installation, the solution is always named like so:

[package name]v[major version].[minor version].wsp

Let’s move on to the JSOM example. In other to write it, there where bits and pieces we’ve copied related to the binary encoding and uploading of files and we’ve tried to give credit where possible, but we’re pretty sure we’re missing an acknowledgement. Rest assured, this wasn’t because of any bad intentions.

We found doing the same thing in JSOM is harder, especially when you’re trying to do it before creating a POC in C#. Part of the reason for this is that the JSOM DesignPackageInfo documentation isn’t really helpful at the time of writing, but it’s here: https://msdn.microsoft.com/en-us/library/office/jj954421.aspx in case you want to take a look.

This example involves an HTML page that uploads the site template and activates it in the Solution Gallery. It goes something like this:

  1. You need a reference to the sp.publishing.js library, because it contains the DesignPackageInfo class.
  2. Include a file control to allow end users to upload the site template.
  3. Use a client-side FileReader object to read the binaries.
  4. Upload the file to a SharePoint library and give it a title. Failing to give the file a title may result in situations where the eventual package name is [a guid consisting of 0’s]v.[major].[minor]. We’ve seen this happen on several occasions and it seems to be a problem that doesn’t happen in C#/CSOM.
  5. Use the DesignPackageInfo class to install and activate the solution.






  











Upload en Activeer Solution


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值