gpt4 book ai didi

sharepoint-2013 - 使用 CSOM 安装 Sharepoint 应用程序

转载 作者:行者123 更新时间:2023-12-04 07:02:57 24 4
gpt4 key购买 nike

有谁知道是否可以使用 CSOM 在 sharepoint 站点上自动安装特定的应用程序?我们有在 sharepoint 上创建新站点的机制,我们需要的是使用 CSOM 在每个新创建的站点上自动安装特定 Sharepoint 应用程序的过程。

任何人都可以提供代码示例或一些有用的链接吗?

最佳答案

SharePoint CSOM API Web class公开以下方法:

  • Web.LoadAndInstallApp method - 将应用程序包上传并安装到此站点
  • Web.LoadAndInstallAppInSpecifiedLocale method - 上传和
    在指定的语言环境中安装应用程序包
  • Web.LoadApp method - 上传一个应用包并创建一个
    来自它的实例

  • 如何通过 CSOM 部署(上传和安装)SharePoint 应用程序
    /// <summary>
    /// Deploy SP App
    /// </summary>
    /// <param name="context">Client context</param>
    /// <param name="appFullPath">Full path to app file (.app)</param>
    /// <returns></returns>
    private static AppInstance DeployApp(ClientContext context, string appFullPath)
    {
    using (var packageStream = System.IO.File.OpenRead(appFullPath))
    {
    var appInstance = context.Web.LoadAndInstallApp(packageStream);
    context.Load(appInstance);
    context.ExecuteQuery();
    return appInstance;
    }
    }

    用法

    以下示例演示如何将 SP 应用程序部署到 SharePoint Online
    using (var ctx = new ClientContext(url))
    {
    ctx.Credentials = new SharePointOnlineCredentials(userName, securePassword);
    var appInstance = DeployApp(ctx, @"C:\Packages\SPApp.app");
    if (appInstance != null && appInstance.Status == AppInstanceStatus.Initialized)
    {
    Console.WriteLine("App was installed.");
    }
    }

    更新

    上述解决方案旨在将应用程序安装到 developer site .

    以下解决方案演示了如何将应用安装到 non developer site
    public class SPAppManager
    {
    /// <summary>
    /// Deploy SP App
    /// </summary>
    /// <param name="context">Client context</param>
    /// <param name="appFullPath">Full path to app file (.app)</param>
    /// <returns></returns>
    public static AppInstance Deploy(ClientContext context, string appFullPath)
    {
    EnsureDeveloperFeature(context);
    using (var packageStream = System.IO.File.OpenRead(appFullPath))
    {
    var appInstance = context.Web.LoadAndInstallApp(packageStream);
    context.Load(appInstance);
    context.ExecuteQuery();
    return appInstance;
    }
    }


    /// <summary>
    /// Ensure Developer Feature
    /// </summary>
    /// <param name="ctx"></param>
    private static void EnsureDeveloperFeature(ClientContext ctx)
    {
    var result = ctx.LoadQuery(ctx.Site.Features.Where(f => f.DefinitionId == DeveloperFeatureId));
    ctx.ExecuteQuery();
    if (result.Any()) return;
    var feature = ctx.Site.Features.Add(DeveloperFeatureId,true, FeatureDefinitionScope.None);
    ctx.ExecuteQuery();
    }


    private static readonly Guid DeveloperFeatureId = new Guid("e374875e-06b6-11e0-b0fa-57f5dfd72085");
    }

    Gist: SPAppManager.cs

    用法
     using (var ctx = GetContext(webUri,userName,password))
    {
    var appInstance = SPAppManager.Deploy(ctx, @"C:\Packages\SPApp.app");
    if (appInstance != null && appInstance.Status == AppInstanceStatus.Initialized)
    {
    Console.WriteLine("App was installed.");
    }
    }

    关于sharepoint-2013 - 使用 CSOM 安装 Sharepoint 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25260197/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com