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

时间:2014-11-17 10:17:21

标签: sharepoint-2013 web-parts sharepoint-apps

我需要将一个客户端Webpart从提供商托管的应用程序添加到其部署到的主机Web中的页面上。我尝试使用客户端对象模型的有限WebPart管理器来实现此目的,但这仅适用于属于.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();

1 个答案:

答案 0 :(得分:3)

这是您需要执行的步骤。

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

        /// <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();
        }
        
      3. 并调用方法:

        Microsoft.SharePoint.Client.File publishingPage = Helpers.CreatePublishingPage(cc, "Solicitudes", "BlankWebPartPage");
        Helpers.AddRegistroDeSolicitudesWebpartToWebPartPage(cc, publishingPage);