Search This Blog

2011-02-05

Create a simple webpart

Step 1-Create a website "Website1"
Step 2-Add reference "windows sharepoint service"
Step 3-Add a Web user control "SampleUserControl.ascx" in your project
Step 4-HTML code of the user control will be as follows
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SampleUserControl.ascx.cs"
Inherits="SampleUserControl" %>
<table>
<tr>
<td>
Put a URL of your Site :
</td>
<td>
<asp:TextBox ID="txtSiteUrl" Text="http://vpc1:2010/sites/SharepointPoC" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSPList" runat="server" Text="Find Splist"
onclick="btnSPList_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="txtOutput" runat="server" Text="output Window"
TextMode="MultiLine" Width="100%"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
File Viewer Control
</td>
</tr>
<tr>
<td colspan="2">
<asp:TreeView NodeStyle-HorizontalPadding="6" ID="treeSitesFiles" runat="server" EnableViewState="false" />
</td>
</tr>
</table>



The C# code will be as follows


using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

public partial class SampleUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SPWeb site = SPContext.Current.Web;
SPFolder rootFolder = site.RootFolder;
TreeNode rootNode = new TreeNode(site.Title, "", @"\_layouts\images\FPWEB16.GIF");
rootNode.NavigateUrl = site.ServerRelativeUrl;
LoadFolderNodes(rootFolder, rootNode);
treeSitesFiles.Nodes.Add(rootNode);
treeSitesFiles.ExpandDepth = 1;
}
protected void LoadFolderNodes(SPFolder folder, TreeNode folderNode)
{

foreach (SPFolder childFolder in folder.SubFolders)
{
TreeNode childFolderNode = new TreeNode(childFolder.Name, "", @"\_layouts\images\FOLDER16.GIF");
childFolderNode.NavigateUrl = childFolder.ServerRelativeUrl;
LoadFolderNodes(childFolder, childFolderNode);
folderNode.ChildNodes.Add(childFolderNode);
}

foreach (SPFile file in folder.Files)
{
TreeNode fileNode;
if (file.CustomizedPageStatus == SPCustomizedPageStatus.Uncustomized)
{
fileNode = new TreeNode(file.Name, "", @"\_layouts\images\NEWDOC.GIF");
fileNode.NavigateUrl = file.ServerRelativeUrl;
}
else
{
fileNode = new TreeNode(file.Name, "", @"\_layouts\images\RAT16.GIF");
fileNode.NavigateUrl = file.ServerRelativeUrl;
}
folderNode.ChildNodes.Add(fileNode);
}


}
protected void btnSPList_Click(object sender, EventArgs e)
{
txtOutput.Text = "";
string sitePath = txtSiteUrl.Text;
// enter object model through site collection.
SPSite siteCollection = new SPSite(sitePath);
// obtain reference to top-level site.
SPWeb site = siteCollection.RootWeb;
txtOutput.Text = "Site Url is " + site.Url + "\n";
// enumerate through lists of site
foreach (SPList list in site.Lists)
{
txtOutput.Text = txtOutput.Text + "\n" + list.Title;
//Console.WriteLine(list.Title);
}
// clean up by calling Dispose.
site.Dispose();
siteCollection.Dispose();

}
}


Step 5-Add a class library project "MyWebPartClass" to your solution
Step 6-Add reference "Windows sharepoint service"
Step 7-Add a class "MyWebprtClass.cs" to your project
Step 8-The code will be as follows
using System;
using System.Collections.Generic;
using System.Web;
using Microsoft.SharePoint.WebPartPages;

///
/// Summary description for MyWebprtClass
///

namespace MyWebPartClass
{
public class MyWebprtClass : WebPart
{
System.Web.UI.Control _myControl;
String err;
public MyWebprtClass()
{
//
// TODO: Add constructor logic here
//
}
protected override void CreateChildControls()
{
try
{
this.Controls.Clear();
_myControl = this.Page.LoadControl("~\\_layouts\\SampleUserControl.ascx");
this.Controls.Add(_myControl);
}
catch (Exception ex)
{

}


}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
try
{
// _myControl = this.Page.LoadControl("~\\_layouts\\SampleUserControl.ascx");
_myControl.RenderControl(writer);
}
catch (Exception ex)
{
writer.Write(ex.Message.ToString());
}

}
}
}

Step 9-Add a xml "MyWebPartClass.xml" to your project and add the following line of code-
<?xml version="1.0" encoding="utf-8" ?>
<webParts>
<webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
<metaData>
<type name="MyWebPartClass.MyWebprtClass, MyWebPartClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30534ee939de1c1d" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
</metaData>
<data>
<properties>
<property name="Title" type="string">SmartParticles Web Part</property>
<property name="Description" type="string">
A demonstration using WebParticles in
a SharePoint WebPart
</property>
<property name="ChromeType">TitleOnly</property>
<property name="ChromeState">Normal</property>
<property name="ItemLimit" type="int">15</property>
<property name="ItemStyle" type="string">Default</property>
</properties>
</data>
</webPart>
</webParts>


Step 10-Add a strong name to your calss library project,build it and install it to GAC
Step 11-Copy your aspx and cs page to the following folder
C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS
Step 11-Open the web config of your sharepoint site and paste the following line to your SafeControls node

<SafeControl Assembly="MyWebPartClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=30534ee939de1c1d" Namespace="MyWebPartClass" TypeName="*" Safe="True" />

Step 12-Go to Site Settings->Galleries ->Web Parts
Click upload and browse the "MyWebPartClass.xml" ,click OK

Then go to any web part zone and add your webpart "MyWebPartClass.xml" from the custom section.

Output will be as follows-

No comments: