在沙盒解决方案中激活功能期间将 Web 部件部署到页面

发布于 2024-09-08 07:09:42 字数 2135 浏览 3 评论 0 原文

在过去的几天里,我一直在用头撞墙。我有一个非常简单的要求,但我看不出如何在沙盒解决方案中实现它。我很沮丧,因为我认为这是一个关键的限制,也会让我在未来的解决方案中感到悲伤。

以下是场景。

我有一个现有网站,我希望能够激活一项功能,然后作为激活的一部分,将 Web 部件拖放到某些特定页面上。就我而言,我希望能够将其放到列表的“AllItems.aspx”页面上,但我同样在将其添加到 default.aspx 主页时遇到问题。不管怎样我都会很高兴。

注意:我希望能够在现有站点上激活此功能,我不想创建新的站点定义。

这是我尝试过的:

  1. 功能接收器:不行。您不能使用 SPLimitedWebPartManager 类,并且 SPWebPartManager 类只能在其实例化的页面上下文中工作。所以基本上,你不能在代码中做到这一点。

  2. XML 模块:我认为这是最有可能的前进道路,但它似乎总是想向站点提供文件。例如,如果我想将 Web 部件添加到主页 (default.aspx),那么似乎没有办法只获取对该页面的引用,它总是想要用新文件覆盖它。但我不想这样做,我只想向现有页面添加一个Web 部件。请参阅下面的我的元素 XML,我尝试了“IgnoreIfAlreadyExists”的各种组合并删除 Path 属性,并将其设置为空白。

  3. 委托控制:沙箱中不允许。

  4. 我实际上希望能够将 Web 部件添加到列表的“AllItems.aspx”形式,因此我还尝试通过 SCHEMA.XML 文件添加 Web 部件,如下所述:通过 SCHEMA.XML 添加 Web 部件 但这也不起作用。我应该能够通过更严格地自定义列表定义来做到这一点,但这似乎有点过头了,在排除其他选项之前我不愿意这样做。

因此,我能看到的唯一简洁方式是某种“配置”页面,然后通过 Web 服务来完成此操作。但这很痛苦,用户需要“激活”该功能,然后在我的配置页面上再次“激活”。

不管怎样,这可能只是我遗漏了一些明显的东西。我在 SharePoint 上摆弄 XML 已经有一段时间了,这是我的第一个沙盒解决方案。真的希望其他人已经破解了这个难题。

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Pages">
    <File Path="" Url="default.aspx">
      <AllUsersWebPart WebPartZoneID="Left" WebPartOrder="1">
        <![CDATA[
              <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image">
                <Assembly>(Assembly), Version=1.0.0.0, Culture=neutral, PublicKeyToken=12baeb5f1600543c</Assembly>
                <TypeName>(Typename)</TypeName>
                <FrameType>None</FrameType>
                <Title>ListAnnouncement</Title>
              </WebPart>
            ]]>
      </AllUsersWebPart>
    </File>
</Module>
</Elements>

I've been hitting my head against a wall on this one for the last couple of days. I have a pretty simple requirement but I cant see how it can be achieved within a Sandboxed solution. I'm depressed because I think its a critical limitation that will cause me grief in future solutions too.

Here is the scenario.

I have an existing site and I want to be able to activate a feature, and then as part of that activation, drop web parts onto some specific pages. In my case I want to be able to drop it onto the "AllItems.aspx" page of a list, but I'm equally having trouble adding it to the default.aspx home page. I would be happy either way.

Note: I want to be able to activate this on existing site, I dont want to create a new Site Definition.

Here is what I have tried:

  1. Feature Receiver: No go. You cant use the SPLimitedWebPartManager class, and the SPWebPartManager class works only within the context of the page where its instantiated. So bascially, you cant do this in code.

  2. XML Module: I think this is the most likely path forward, however it seems to always want to provision a file to the site. For example, if I want to add a web part to the home page (default.aspx), then there doesn't seem to be a way to just get a reference to the page, it always wants to overwrite it with a new file. But I dont want to do that, I just want to add a Web Part to the existing page. See my elements XML below, I have tried various combinations of "IgnoreIfAlreadyExists" and removing the Path attribute, and setting it blank.

  3. Delegate Control: Not allowed in the sandbox.

  4. I actually want to be able to add a web part to the "AllItems.aspx" form of a list, so I also tried to add the web part via the SCHEMA.XML file as described here: Add Web Part through the SCHEMA.XML but this didn't work either. I should be able to do it by more heavily customising the List Definition, but that seems a little over the top and I'm reluctant to do that before ruling out the other options.

So, the only Neat way I can see through it is some sort of "Configuration" page, which then does it via the Web Services. But this is a pain, with the user having "Activate" the feature and then "Activate" again on my configuration page.

Anyway, it may just be that I'm missing something obvious. Been a while between XML fiddling on SharePoint for me, and this is my first Sandbox solution. Really hope someone else has cracked this nut.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="Pages">
    <File Path="" Url="default.aspx">
      <AllUsersWebPart WebPartZoneID="Left" WebPartOrder="1">
        <![CDATA[
              <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image">
                <Assembly>(Assembly), Version=1.0.0.0, Culture=neutral, PublicKeyToken=12baeb5f1600543c</Assembly>
                <TypeName>(Typename)</TypeName>
                <FrameType>None</FrameType>
                <Title>ListAnnouncement</Title>
              </WebPart>
            ]]>
      </AllUsersWebPart>
    </File>
</Module>
</Elements>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女情怀诗 2024-09-15 07:09:42

您可以使用完全信任代理将 Web 部件添加到现有页面。引用MSDN:

“沙盒代理是使您能够完全信任的组件
沙盒解决方案可用的功能。”

简而言之,您只需创建一个单独的非沙盒程序集,其中包含按正常方式在指定 URL 上添加 Web 部件的方法(即使用 SPLimitedWebPartManager 类)然后,您在沙盒解决方案中引用并注册<​​/strong>此代理程序集,这与使用 Web 服务实际上没有什么不同:

如何:创建并注册沙盒代理

这里有人已经完成了此操作:在沙盒环境中以编程方式添加 Web 部件

我正在尝试执行此操作与要部署到 SharePoint Online 的沙箱解决方案中的情况完全相同 这被证明是有点麻烦,因为您无法将功能范围限制到服务器场级别 - 但您不应该立即遇到此问题。 - 场所环境。让我知道你过得怎么样...

You can add web parts to existing pages using a full-trust proxy. To quote MSDN:

"Sandbox proxies are components that enable you to make full-trust
functionality available to sandboxed solutions."

In a nutshell, you just need to create a seperate non-sandboxed assembly that contains methods to add web parts on a specified URL as per normal (i.e. using the SPLimitedWebPartManager class). You then reference and register this proxy assembly in your sandboxed solution -- not unlike consuming a web service really.

All is detailed here: How to: Create and Register a Sandbox Proxy

And here is someone who has already done it: Add Web Part Programmatically in Sandbox Environment

I'm trying to do this exact same thing in a sandbox solution that is to be deployed to SharePoint Online. This proves to be a little bit of a pickle because you can't scope features to the farm level -- but you shouldn't have this problem in an on-premise environment. Let me know how you get on...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文