Intro
In this tutorial we will be expanding upon the first post in which we created and deployed a very simple web part. We will be dealing with Windows SharePoint Services Objects in this tutorials and I would recommend, insist and beg that you read this MSDN Best Practices article http://msdn.microsoft.com/en-us/library/aa973248.aspx
What we want to achieve:
Prerequisites:
We need to open up the basic web part project:
3 options here:
- You have followed the first tutorial and you open up the project (Well done!)
- You follow the first tutorial from here and end up with the project (Nice work!)
- You download the project from the first tutorial here (....if you get time work through the tutorial!)
Lets get going:
- The first project we created was a basic ASP.NET web part and we did not have to use any of the SharePoint object model in our code. In this tutorial we will be so we need to add our reference to Microsoft.SharePoint.dll
- OK, make sure you have the following using statements
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Text;
- Lets add some code to get the current SPSite in our CreateChildControls method (SPSite is the site collection, not the actual Web Page, that is the SPWeb!). We will be ensuring we are disposing of our objects correctly so we will be making use of the using statement
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Text;
namespace TheWebPart
{
public class SimpleWebPart : WebPart
{
protected override void CreateChildControls()
{
// Literal Control used to display our information in our Web Part
LiteralControl literalControl = new LiteralControl();
// Use the SPUtility to get the Current Page URL Path using the current Context of the Web Part
string strPageUrl = SPUtility.GetPageUrlPath(Context);
// Object will be disposed correctly
using (SPSite spSite = new SPSite(strPageUrl))
{
//Code will be going in here to get information from our SPSite
}
// Add our control to the Web Part
Controls.Add(literalControl);
}
}
}
- As you will see we are disposing of our spSite object nicely and also you will see we are not doing anything with it at the moment. Add the following code so it all looks like this:
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.Text;
namespace TheWebPart
{
public class SimpleWebPart : WebPart
{
protected override void CreateChildControls()
{
// String Builder to hold our SPSite Data
StringBuilder sb = new StringBuilder();
// Literal Control used to display our information in our Web Part
LiteralControl literalControl = new LiteralControl();
// Use the SPUtility to get the Current Page URL Path using the current Context of the Web Part
string strPageUrl = SPUtility.GetPageUrlPath(Context);
// Object will be disposed correctly
using (SPSite spSite = new SPSite(strPageUrl))
{
// Get the Features available on the Site Collection
string strFeature = string.Format("This site collection has {0} features", spSite.Features.Count);
sb.AppendLine(strFeature);
sb.AppendLine("<br/>");
// Get all of the SPWebs in the Site Collection
string strWebs = string.Format("This site collection has {0} web(s)", spSite.AllWebs.Count);
sb.AppendLine(strWebs);
sb.AppendLine("<br/>");
// Gets the owner of the Site Collection
string strOwner = string.Format("This site collection is owned by {0}", spSite.Owner);
sb.AppendLine(strOwner);
}
// Add the Literal Controls Text
literalControl.Text = sb.ToString();
// Add our control to the Web Part
Controls.Add(literalControl);
}
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
- We are now getting the AllWebs, Feature and Owner information from our SPSite Object and entering it all into our Literal control via the use of a StringBuilder.
- Compile your code
- The following method is a quick and easy way to update your web part but should only be used in a dev environment. These steps can be put into a post build event if you wanted to.
- Drag and drop your compiled DLL into the GAC (C:\Windows\Assembly)
- Perform an IISReset
The web part should now show our updated web part!