简体   繁体   English

如何将自定义构建的webpart添加到列表定义项目中的自定义视图aspx页面

[英]How to add a custom built webpart to a custom view aspx page in list definition project

I have developed a list definition project and created a custom view aspx page to be the default view of the list. 我已经开发了一个列表定义项目,并创建了一个自定义视图aspx页面作为列表的默认视图。 I would now like to add a custom webpart to be displayed in the custom view aspx page. 我现在想添加一个自定义webpart以显示在自定义视图aspx页面中。 Any ideas of how it can be done? 有关如何做到的任何想法?

On SharePoint 2010 you can add a custom webpart or SharePoint WebPart in the same view definition inside the Schema.xml of the List definition (I'm not talking about List instance). 在SharePoint 2010上,您可以在List定义Schema.xml内的同一视图定义中添加自定义Webpart或SharePoint WebPart(我不是在讨论List实例)。

Although isn't directly explained by MSDN, and if you look at the View Schema you can't see a '<WebParts>' definition, you can actually do something like this: 虽然没有MSDN直接解释,如果您查看View Schema,您看不到“<WebParts>”定义,您实际上可以执行以下操作:

<View BaseViewID="7" Type="HTML" WebPartZoneID="Main" DisplayName="$Resources:CILSS_defaultView" DefaultView="TRUE" MobileView="FALSE" MobileDefaultView="FALSE" SetupPath="pages\viewpage.aspx" ImageUrl="/_layouts/images/generic.png" Url="CI_Projects.aspx">
        <Toolbar Type="Standard" />
        <XslLink Default="TRUE">main_cilss.xsl</XslLink>
        <RowLimit Paged="TRUE">30</RowLimit>
        <ViewFields>
          <FieldRef Name="Attachments"></FieldRef>
          <FieldRef Name="LinkTitle"></FieldRef>
          <FieldRef Name="CILSS_ProjectDescription"></FieldRef>
        </ViewFields>
        <Query>
          <OrderBy>
            <FieldRef Name="LinkTitle" Ascending="TRUE"></FieldRef>
          </OrderBy>
        </Query>
        <ParameterBindings>
          <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
          <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(wss,noXinviewofY_DEFAULT)" />
        </ParameterBindings>
        <WebParts>
          <AllUsersWebPart WebPartZoneID="Main" WebPartOrder="2">
            <![CDATA[<WebPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.microsoft.com/WebPart/v2">
  <Title>Default Home Webpart</Title>
  <FrameType>None</FrameType>
  <Description>Use for formatted text, tables, and images.</Description>
  <IsIncluded>true</IsIncluded>
  <ZoneID>Left</ZoneID>
  <PartOrder>0</PartOrder>
  <FrameState>Normal</FrameState>
  <Height />
  <Width />
  <AllowRemove>true</AllowRemove>
  <AllowZoneChange>true</AllowZoneChange>
  <AllowMinimize>true</AllowMinimize>
  <AllowConnect>true</AllowConnect>
  <AllowEdit>true</AllowEdit>
  <AllowHide>true</AllowHide>
  <IsVisible>true</IsVisible>
  <DetailLink />
  <HelpLink />
  <HelpMode>Modeless</HelpMode>
  <Dir>Default</Dir>
  <PartImageSmall />
  <MissingAssembly>Cannot import this Web Part.</MissingAssembly>
  <PartImageLarge>/_layouts/images/mscontl.gif</PartImageLarge>
  <IsIncludedFilter />
  <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
  <TypeName>Microsoft.SharePoint.WebPartPages.ContentEditorWebPart</TypeName>
  <ContentLink xmlns="" />
  <Content xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor">
  <Value>&lt;div&gt;Create and view Project documents:&lt;/div&gt;
&lt;br/&gt;</Value></Content>
  <PartStorage xmlns="http://schemas.microsoft.com/WebPart/v2/ContentEditor" />
</WebPart>]]>
          </AllUsersWebPart>
        </WebParts>
      </View>

This view definition includes a Content Editor Webpart in the list view called 'CI_Projects.aspx' Remember to assign the WebPartZoneId and assign a BaseViewID different from the default views included in the List definition. 此视图定义在列表视图中包含名为“CI_Projects.aspx”的内容编辑器Webpart。请记住分配WebPartZoneId并分配与List定义中包含的默认视图不同的BaseViewID。

The webpart will provisioned on every List you will create with this List Definition. webpart将在您将使用此列表定义创建的每个列表上进行配置。 (obviously only on the named view.aspx you define). (显然只在你定义的命名view.aspx上)。

You can use a feature receiver and manage the addition of web parts during feature activation. 您可以使用功能接收器并在功能激活期间管理Web部件的添加。 Here is a bit of a stub to help get you started. 这里有一些存根可以帮助您入门。

In the feature activated method call code similar to the following: 在功能激活方法中调用代码类似于以下内容:

        // Add web parts to the .aspx page that was provisioned
        SPFile page = site.GetFile(<<page url>>);

        Hashtable hashWebPartsOnPage = new Hashtable();

        using (SPLimitedWebPartManager mgr = page.GetLimitedWebPartManager(PersonalizationScope.Shared))
        {
            try
            {
                // First, let's clear the existing web parts off of the form
                foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in mgr.WebParts)
                {
                    hashWebPartsOnPage.Add(webPart.ID, webPart);
                }

                foreach (string webPartName in hashWebPartsOnPage.Keys)
                {
                    System.Web.UI.WebControls.WebParts.WebPart webPart = hashWebPartsOnPage[webPartName] as System.Web.UI.WebControls.WebParts.WebPart;
                    mgr.DeleteWebPart(webPart);
                }

                // Add Part to the proper Zone
                MyWebPart myWebPart = new MyWebPart();
                //set web part properties
                mgr.AddWebPart(myWebPart, "FullPage", 1);
            }
            finally
            {
                mgr.Web.Dispose();
            }

        }
        page.Update();

Well i would like to access the list definition view page which i don't think will be accessible using the above site.GetFile() method. 好吧,我想访问列表定义视图页面,我认为不会使用上面的site.GetFile()方法访问。 Any other options you think we can use to achieve it. 您认为我们可以使用任何其他选项来实现它。 I have tried the following steps: 我尝试了以下步骤:

  1. Copy the OOB “ViewPage.aspx” and place it in the same folder as “CustomViewPage.aspx” 复制OOB“ViewPage.aspx”并将其放在与“CustomViewPage.aspx”相同的文件夹中

  2. Create the custom web part which shows the items of the view with your custom approach. 创建自定义Web部件,使用您的自定义方法显示视图的项目。

  3. Create a feature to provision the “CustomViewpage.aspx” along with your custom web part. 创建一个功能以配置“CustomViewpage.aspx”以及您的自定义Web部件。

  4. Create a custom list definition. 创建自定义列表定义。 In the schema.xml file of the definition for all the views mention the “SetupPath” to refer your “CustomViewPage.aspx”. 在所有视图的定义的schema.xml文件中,提到“SetupPath”以引用“CustomViewPage.aspx”。 With this step all the views mentioned in the list definition will use the “CustomViewPage.aspx” to create the view pages and as well all the views which you create after the creation of the list will use the “customViewPage.aspx” as the base page to create the view pages. 在此步骤中,列表定义中提到的所有视图将使用“CustomViewPage.aspx”来创建视图页面,以及创建列表后创建的所有视图将使用“customViewPage.aspx”作为基础用于创建视图页面的页面。

Step 1: 步骤1:

Just copy the OOB “ViewPage.aspx” and place it in the C:\\Program Files\\Common Files\\Microsoft Shared\\web server extensions\\12\\TEMPLATE\\Pages folder as “CustomViewPage.aspx” 只需复制OOB“ViewPage.aspx”并将其放在C:\\ Program Files \\ Common Files \\ Microsoft Shared \\ web服务器extensions \\ 12 \\ TEMPLATE \\ Pages文件夹中作为“CustomViewPage.aspx”

Step 2: 第2步:

Create the custom web part which shows the items of the view with your custom approach. 创建自定义Web部件,使用您的自定义方法显示视图的项目。 Basically use the GetViewfromUrl(“Lists//”) method to get the view and then retrieve the items in the web part. 基本上使用GetViewfromUrl(“Lists //”)方法获取视图,然后检索Web部件中的项目。 The code snippet for the custom web part looks like follows : 自定义Web部件的代码段如下所示:

SPList list = SPContext.Current.List; SPList list = SPContext.Current.List;

String strViewName = String strViewName =

SPView view = SPContext.Current.Web.GetViewFromUrl("Lists/list/ "+strViewName); SPView视图= SPContext.Current.Web.GetViewFromUrl(“Lists / list /”+ strViewName);

SPListItemCollection listItems = list.GetItems(view); SPListItemCollection listItems = list.GetItems(view);

. .

.

.

After getting the list items then do your custom approach of displaying the items. 获取列表项后,然后执行显示项目的自定义方法。

Build the web part DLL and place it in the GAC. 构建Web部件DLL并将其放在GAC中。

Step 3: 第3步:

Create a feature to provision the “CustomViewPage.aspx” along with your custom web part. 创建一个功能以配置“CustomViewPage.aspx”以及您的自定义Web部件。 By activating this feature the “Customviewpage.aspx” will be embedded with your custom web part. 通过激活此功能,“Customviewpage.aspx”将嵌入您的自定义Web部件。 The elements manifest file content looks like follows : 元素清单文件内容如下所示:

<File Url="Pages/CustomViewPage.aspx" Type="Ghostable">

  <AllUsersWebPart WebPartZoneID="Main" WebPartOrder="1">

    <![CDATA[

               <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2">

                    <Assembly>CustomWebpartforView, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e3483ddc8b0d437a</Assembly>

                    <TypeName> CustomWebpartforView.MyCustomview</TypeName>

                    <FrameType>None</FrameType>

                                            <IsVisible>true</IsVisible>

                    <Title>Custom Web part for View</Title>                                 

               </WebPart>

               ]]>

  </AllUsersWebPart>

</File>

This step gives me error for the source file path which i modified by giving the Path attribute in the File Tag 此步骤给出了我通过在文件标记中给出Path属性修改的源文件路径的错误

Step 4: 第4步:

Now the base custom view page is available along with the web part. 现在,基本自定义视图页面与Web部件一起可用。 Now how to integrate the custom view page with the list definition. 现在,如何将自定义视图页面与列表定义集成。 Create a custom List definition and for all the views defined in the schema mention the “SetupPath” to use the “CustomViewPage.aspx”. 创建自定义列表定义,并为架构中定义的所有视图提及“SetupPath”以使用“CustomViewPage.aspx”。 Now the custom view page will be used as the base view page for all the views defined in the list definition and as well the views created later also will be using the “CustomViewpage.aspx” as the base page. 现在,自定义视图页面将用作列表定义中定义的所有视图的基本视图页面,以及稍后创建的视图也将使用“CustomViewpage.aspx”作为基页。 The sample snippet for declaring the views inside the schema.xml is as follows : 用于声明schema.xml内部视图的示例代码段如下:

Install and activate the feature of your custom list definition. 安装并激活自定义列表定义的功能。

Now with all these steps, when you create a list based on this custom list definition then all the views created will be using the “CustomViewPage.aspx” to create the view pages and your custom web part will be rendered instead of the OOB dataformwebpart. 现在,通过所有这些步骤,当您基于此自定义列表定义创建列表时,创建的所有视图将使用“CustomViewPage.aspx”来创建视图页面,并且将呈现您的自定义Web部件而不是OOB dataformwebpart。 The views which you will be creating after the creation of the List (Through UI, Through Object Model) will also use the “CustomViewpage.aspx” as the base page to create view pages. 在创建List(通过UI,通过对象模型)之后创建的视图也将使用“CustomViewpage.aspx”作为基页来创建视图页面。

For some reason i am still unable to see the CustomViewPage.aspx with my custom webpart. 由于某种原因,我仍然无法使用我的自定义webpart看到CustomViewPage.aspx。 Am i missing somthing or there is something missing in this article. 我错过了什么或者本文中缺少某些东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM