【问题标题】:Adding a Client Web Part (App Part) to a sharepoint page programmatically以编程方式将客户端 Web 部件(应用程序部件)添加到共享点页面
【发布时间】:2015-01-14 05:27:24
【问题描述】:

我需要将来自提供商托管应用程序的客户端 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();

【问题讨论】:

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


    【解决方案1】:

    这是您需要执行的步骤。

    1. 您需要从浏览器将您的应用部分添加到任何页面
    2. 在“Web 部件属性”窗格中,展开“高级”部分,将“导出模式”设置为“导出所有数据”,然后单击“确定”。

    1. 在应用程序部件中,单击 Web 部件标题旁边的下拉箭头,然后单击导出

    1. 将 .webpart 文件复制到项目中(例如:模板文件夹)
    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();
      }
      

    并调用方法:

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

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 2010-10-08
      • 2011-01-18
      • 1970-01-01
      • 2012-01-15
      • 2013-02-28
      • 2021-07-01
      • 1970-01-01
      • 2014-10-28
      相关资源
      最近更新 更多