Sharepoint 2013 以编程方式将客户端Web部件(应用程序部件)添加到sharepoint页面

Sharepoint 2013 以编程方式将客户端Web部件(应用程序部件)添加到sharepoint页面,sharepoint-2013,web-parts,sharepoint-apps,Sharepoint 2013,Web Parts,Sharepoint Apps,我需要将提供商托管应用程序中的客户端Web部件添加到其部署到的主机Web中的页面上。我曾尝试使用客户端对象模型的有限Web部件管理器来实现这一点,但这仅适用于属于.dwp或.WebPart文件的xml数据。我使用了下面的代码。是否有解决方法,从站点获取应用程序部件文件并将其添加到Sharepoint页面 ClientContext clientconteext = new ClientContext("My Server URL"); Microsoft.Shar

我需要将提供商托管应用程序中的客户端Web部件添加到其部署到的主机Web中的页面上。我曾尝试使用客户端对象模型的有限Web部件管理器来实现这一点,但这仅适用于属于.dwp或.WebPart文件的xml数据。我使用了下面的代码。是否有解决方法,从站点获取应用程序部件文件并将其添加到Sharepoint页面

        ClientContext clientconteext = new ClientContext("My Server URL");
        Microsoft.SharePoint.Client.File page = clientconteext.Web.GetFileByServerRelativeUrl("/sites/MySite/SitePages/Home.aspx");

        clientconteext.Load(clientconteext.Web);
        clientconteext.Load(page);
        clientconteext.ExecuteQuery();
        LimitedWebPartManager lwp= page.GetLimitedWebPartManager(PersonalizationScope.Shared);
        string webpartxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Elements xmlns=\"http://schemas.microsoft.com/sharepoint/\"><WebPartPages:ClientWebPart runat=\"server\" FeatureId=\"5b1a14dd-8dbe-4963-8612-e7918e7fbc9a\" ProductWebId=\"5b1a14dd-8dbe-4963-8612-e7918e7fbc9a\" WebPartName=\"HomePageAppPart\" Title=\"Home App Part\" Description=\"WebPart Description\" WebPart=\"true\"></WebPartPages:ClientWebPart></Elements>";
        WebPartDefinition wpd = lwp.ImportWebPart(webpartxml);
        lwp.AddWebPart(wpd.WebPart, "Right", 1);
        clientconteext.ExecuteQuery();
ClientContext ClientContext=newclientcontext(“我的服务器URL”);
Microsoft.SharePoint.Client.File page=ClientContext.Web.GetFileByServerRelativeUrl(“/sites/MySite/SitePages/Home.aspx”);
加载(clientcontext.Web);
clientcontext.Load(第页);
ClientContext.ExecuteQuery();
LimitedWebPartManager lwp=page.GetLimitedWebPartManager(PersonalizationScope.Shared);
字符串webpartxml=“”;
WebPartDefinition wpd=lwp.ImportWebPart(webpartxml);
lwp.AddWebPart(wpd.WebPart,“右”,1);
ClientContext.ExecuteQuery();

这是您需要执行的步骤

  • 您需要将应用程序部件从浏览器添加到任何页面
  • 在“Web部件属性”窗格中,展开“高级”部分,将“导出模式”设置为“导出所有数据”,然后单击“确定”
  • 在应用程序部件中,单击web部件标题旁边的下拉箭头,然后导出
  • 将.webpart文件复制到项目(例如:模板文件夹)
  • 添加此方法以创建发布页面并将应用程序部件添加到其中

    /// <summary>
    /// Create a Publising Page
    /// </summary>
    /// <param name="clientContext">Client context</param>
    /// <param name="pageName">Page Name</param>
    /// <param name="pagelayoutname">Page Layout Name</param>
    public static File CreatePublishingPage(ClientContext clientContext, string pageName, string pagelayoutname)
    {
        var publishingPageName = pageName + ".aspx";
    
        Web web = clientContext.Web;
        clientContext.Load(web);
    
        List pages = web.GetListByUrl("/Pages/");
        clientContext.Load(pages.RootFolder, f => f.ServerRelativeUrl);
        clientContext.ExecuteQuery();
    
        Microsoft.SharePoint.Client.File file =
            web.GetFileByServerRelativeUrl(pages.RootFolder.ServerRelativeUrl + "/" + pageName + ".aspx");
        clientContext.Load(file, f => f.Exists);
        clientContext.ExecuteQuery();
        if (file.Exists)
        {
            file.DeleteObject();
            clientContext.ExecuteQuery();
        }
        PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(clientContext, web);
        clientContext.Load(publishingWeb);
    
        if (publishingWeb != null)
        {
            List publishingLayouts = clientContext.Site.RootWeb.GetListByUrl("/_catalogs/masterpage/");
    
            ListItemCollection allItems = publishingLayouts.GetItems(CamlQuery.CreateAllItemsQuery());
            clientContext.Load(allItems, items => items.Include(item => item.DisplayName).Where(obj => obj.DisplayName == pagelayoutname));
            clientContext.ExecuteQuery();
    
            ListItem layout = allItems.Where(x => x.DisplayName == pagelayoutname).FirstOrDefault();
            clientContext.Load(layout);
    
            PublishingPageInformation publishingpageInfo = new PublishingPageInformation()
            {
                Name = publishingPageName,
                PageLayoutListItem = layout,
            };
    
            PublishingPage publishingPage = publishingWeb.AddPublishingPage(publishingpageInfo);
            publishingPage.ListItem.File.CheckIn(string.Empty, CheckinType.MajorCheckIn);
            publishingPage.ListItem.File.Publish(string.Empty);
            clientContext.Load(publishingPage);
            clientContext.ExecuteQuery();
            return publishingPage.ListItem.File;
        }
        return null;
    }
    
    /// <summary>
    /// Adds the Web Part to the page
    /// </summary>
    /// <param name="clientContext">Client Context</param>
    /// <param name="newWeb">New Web</param>
    public static void AddWebpartToWebPartPage(ClientContext clientContext, File file)
    {
        file.CheckOut();
    
        //Get webparts xml
        string webpart = System.IO.File.ReadAllText(System.Web.Hosting.HostingEnvironment.MapPath(string.Format("~/{0}", "Template/RegistroDeSolicitudes.webpart")));
    
        // Requires Full Control permissions on the Web
        LimitedWebPartManager wpmgr = file.GetLimitedWebPartManager(PersonalizationScope.Shared);
        WebPartDefinition wpd = wpmgr.ImportWebPart(webpart);
        wpmgr.AddWebPart(wpd.WebPart, "Header", 0);
        file.CheckIn(String.Empty, CheckinType.MajorCheckIn);
        file.Publish(string.Empty);
        clientContext.ExecuteQuery();
    }
    
    Microsoft.SharePoint.Client.File publishingPage = Helpers.CreatePublishingPage(cc, "Solicitudes", "BlankWebPartPage");
    Helpers.AddRegistroDeSolicitudesWebpartToWebPartPage(cc, publishingPage);