Inclure un composant WebPart dans une page web sur le site web de compl?ment
Vous pouvez inclure un composant WebPart prêt à l’emploi sur une page du site web d’un complément SharePoint. Toutefois, vous devez éviter que des problèmes surviennent si vous devez mettre à jour le complément.
Pour obtenir un exemple de code illustrant les conseils de cette rubrique, consultez l’article OfficeDev/Core.WebPartOnAppWebPage.
Ajouter un composant WebPart à une page
Create a SharePoint-hosted SharePoint Add-in project in Visual Studio. For details, see Get started creating SharePoint-hosted SharePoint Add-ins.
Open the .aspx file to which you want to add a web part. This topic uses Default.aspx as an example.
Add a WebPartZone to the
<asp:Content>element where you want the web part with markup. Typically, you want to add it to the<asp:Content>whoseContentPlaceHolderIdisPlaceHolderMain. The following is an example.<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server"> <WebPartPages:WebPartZone runat="server" FrameType="TitleBarOnly" ID="HomePage1" Title="loc:full" /> </asp:Content>Avertissement
It is possible to add a web part element, such as WebPartPages:XsltListViewWebPart as a child of the WebPartZone. But this is generally a bad practice in a SharePoint Add-in. If the add-in ever needs to be updated, a web part element inserted in the .aspx file can cause the update to fail in some scenarios with the message "A web part with this ID has already been added to this page." We recommend that you add web parts to the elements manifest for the page as described later in this procedure.
Open the element manifest file for the page. This is usually called elements.xml and is located in the same project folder as the .aspx file.
Dans l’élément File de la page, ajoutez un élément enfant AllUsersWebPart et définissez la propriété WebPartZoneID sur la valeur de la zone de composants WebPart créée sur la page, comme indiqué dans cet exemple.
<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Module Name="Pages"> <File Path="Pages\Default.aspx" Url="Pages/Default.aspx" ReplaceContent="TRUE" > <AllUsersWebPart WebPartZoneID="HomePage1" WebPartOrder="1"> </AllUsersWebPart> </File> </Module> </Elements>Ajoutez un élément CDATA comme enfant de l’élément AllUsersWebPart, puis ajoutez un élément webParts comme enfant de l’élément CDATA, comme indiqué dans cet exemple.
<AllUsersWebPart WebPartZoneID="HomePage1" WebPartOrder="1"> <![CDATA[ <webParts> </webParts> ]]> </AllUsersWebPart>Add webPart markup as a child of the webParts element. The following is an example that adds an XsltListViewWebPart. It assumes that a custom list called "Test List" is part of the same add-in project. For information about how to add a custom list to an add-in web, see Create a provider-hosted add-in that includes a custom SharePoint list and content type.
Notes
Note that the web part does not have an ID property. It is a best practice to include an explicit ID for the web part only in the two cases where it is really required: The web part is being added to a SharePoint wiki page. The web part is one of two or more web parts that are connected.
<webParts> <webPart xmlns="http://schemas.microsoft.com/WebPart/v3"> <metaData> <type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" /> </metaData> <data> <properties> <property name="ListUrl">Lists/{TestList}</property> <property name="IsIncluded">True</property> <property name="NoDefaultStyle">True</property> <property name="Title">{Test List}</property> <property name="PageType">PAGE_NORMALVIEW</property> <property name="Default">False</property> <property name="ViewContentTypeId">0x</property> </properties> </data> </webPart> </webParts>Select F5 to debug the add-in. You should see the web part on the page.