Lets get started!
- Launch Visual Studio 2013 with Administrator privileges.
- Select SharePoint Solutions -> SharePoint 2013 - Visual Web Part and provide name of the project along with the location.
- Provide the url of local SharePoint site for debugging then select Deploy as a sandboxed solution option and hit finish.
- You will see the basic structure of the Visual web part created along with one User Control (ASCX) to handle GUI elements and one Feature to handle packaging (WSP).
- Open the user control (VisualWebPart1.ascx) and add following ASP.NET Grid.
<asp:GridView ID="gvProducts" runat="server" />
- Open the code behind of the user control (VisualWebPart1.ascx.cs) and add the following code:
Add Reference to SharePoint API
Add CreateChildControls() functionusing Microsoft.SharePoint;
Your code should look like this:protected override void CreateChildControls() { SPSite objSite = SPContext.Current.Site; using (SPWeb objWeb = objSite.OpenWeb()) { SPQuery objQuery = new SPQuery(); objQuery.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='Title' /><FieldRef Name='Author' />"; objQuery.ViewFieldsOnly = true; SPList objList = objWeb.Lists["ListA"]; SPListItemCollection oCollection = objList.GetItems(objQuery); gvProducts.DataSource = oCollection.GetDataTable(); gvProducts.DataBind(); } }
- Select Build -> Deploy Solution.
- Open IE and go to the site specified in the step 3. Edit the home page and select Insert -> Web Part. You should see this web part under Custom category, select and add the webpart.
- It should display like this
No comments:
Post a Comment