How to Create a SharePoint Web Part - XML data grid method To create a SharePoint Web Part - XML Data grid Method
Please follow the below steps to do this:
-
Create or open a SharePoint project.
For more information, see SharePoint Project and Project Item Templates.
-
Choose the SharePoint project node in Solution Explorer and then choose Project, Add New Item.
-
In the Add New Item dialog box, expand the SharePoint node, and then choose the 2010 node.
-
In the list of SharePoint templates, choose Web Part.
-
In the Name box, specify a name for the web part, and then choose the Add button.
The web part appears in Solution Explorer.
-
In Solution Explorer, open the code file for the web part that you just created.
For example, if the name of your web part is WebPart1, open WebPart1.vb (in Visual Basic) or WebPart1.cs (in C#).
-
In the code file,
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
namespace SharePointProject1.WebPart1
{
[ToolboxItemAttribute(false)]
public class WebPart1 : WebPart
{
protected override void CreateChildControls()
{
// Define the grid control that displays employee data in the Web Part.
grid = new DataGrid();
grid.Width = Unit.Percentage(100);
grid.GridLines = GridLines.Horizontal;
grid.HeaderStyle.CssClass = "ms-vh2";
grid.CellPadding = 2;
grid.BorderWidth = Unit.Pixel(5);
grid.HeaderStyle.Font.Bold = true;
grid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
// Populate the grid control with data in the employee data file.
try
{
DataSet dataset = new DataSet();
dataset.ReadXml(xmlFilePath, XmlReadMode.InferSchema);
grid.DataSource = dataset;
grid.DataBind();
}
catch (Exception x)
{
errorMessage.Text += x.Message;
}
// Add control to the controls collection of the Web Part.
Controls.Add(grid);
Controls.Add(errorMessage);
base.CreateChildControls();
}
public override WebPartVerbCollection Verbs
{
get
{
WebPartVerb customVerb = new WebPartVerb("Manager_Filter_Verb",
new WebPartEventHandler(CustomVerbEventHandler));
customVerb.Text = verbText;
customVerb.Description = "Shows only employees that are managers";
WebPartVerb[] newVerbs = new WebPartVerb[] { customVerb };
return new WebPartVerbCollection(base.Verbs, newVerbs);
}
}
protected void CustomVerbEventHandler(object sender, WebPartEventArgs args)
{
int titleColumn = 2;
foreach (DataGridItem item in grid.Items)
{
if (item.Cells[titleColumn].Text != "Manager")
{
if (item.Visible == true)
{
item.Visible = false;
}
else
{
item.Visible = true;
}
}
}
if (verbText == "Show Managers Only")
{
verbText = "Show All Employees";
}
else
{
verbText = "Show Managers Only";
}
}
private DataGrid grid;
private static string verbText = "Show Managers Only";
private Label errorMessage = new Label();
protected string xmlFilePath;
[Personalizable(PersonalizationScope.Shared), WebBrowsable(true), WebDisplayName("Path to Employee Data File"), WebDescription("Location of the XML file that contains employee data")]
public string DataFilePath
{
get
{
return xmlFilePath;
}
set
{
xmlFilePath = value;
}
}
}
}
To Create XML ::
-
Paste the following XML into a Notepad file. This XML
file contains the sample data that will appear in the Web Part.
<?xml version="1.0" encoding="utf-8" ?> <employees xmlns="http://schemas.microsoft.com/vsto/samples"> <employee> <name>Rajesh Ganesan</name> <hireDate>2010-05-11</hireDate> <title>Developer</title> </employee> <employee> <name>Siva</name> <hireDate>2010-04-01</hireDate> <title>Developer</title> </employee> <employee> <name>Giri</name> <hireDate>2011-05-01</hireDate> <title>Admin</title> </employee> <employee> <name>Gopal</name> <hireDate>2012-03-04</hireDate> <title>Developerr</title> </employee> <employee> <name>Aravind</name> <hireDate>2013-07-02</hireDate> <title>Manager</title> </employee> </employees>
-
In Notepad, on the menu bar, choose File, Save As.
-
In the Save As dialog box, in the Save as type list, choose All Files.
-
In the File name box, enter data.xml.
-
Choose any folder by using the Browse Folders button, and then choose the Save button.
-
In Visual Studio, choose the F5 key.
The SharePoint site opens.
-
On the Site Actions menu, choose More Options.
-
In the Create page, choose the Web Part Page type, then choose the Create button.
-
In the New Web Part Page page, name the page SampleWebPartPage.aspx, and then choose the Create button.
The Web Part page appears.
-
Select any zone on the Web Part page.
-
At the top of the page, choose the Insert tab, and then choose the Web Part button.
-
In the Categories pane, choose the Custom folder, choose the WebPart1 Web Part, and then choose the Add button.
The Web Part appears on the page.
Output : -
Paste the following XML into a Notepad file. This XML
file contains the sample data that will appear in the Web Part.
Comments