I needed a web part to sum up a numeric column of a list. Just like any other developer, I didn't think to look for one, rather I created my own.
Here are the steps necessary to create my first web part.
Summary of tasks.
- Create Class Library
- Create a List in MOSS
- Deploy Assembly in Sharepoint bin dir
- Edit Sharepoint web.config to allow Sharepoint to use Web Part (SafeControl)
- Make necessary adjustments to fix security issue with using Microsoft.Sharepoint namespace (WSS_minimal vs WSS_medium, etc) in class library
- Test web Part
Create a new class library project in Visual Studio 2005.
Add a reference to System.Web & Microsoft.SharePoint (Microsoft.SharePoint.dll)
The Microsoft.Sharepoint reference can be copied from the MOSS Server.
Your class needs to be derived from the WebPart base class as shown in the following.
So now that we have the basic foundation completed, lets get on with developing our Web Part.
Override the RenderControl method of the WebPart base class, and add a using statement to the Microsft.SharePoint namespace.
Now the code will probably not make much sense right now, and your probably wondering how it's ever going to work with references to a list named "Test", and a column named, "aNumber". So lets create these elements on our Sharepoint Server - before we deploy and test this piece of code.
Create a List in MOSS
Navigate to your development MOSS site and create a list, by viewing all site content and clicking on the "Create" action and then selecting "Custom List".
When creating your list, make sure you name it "Test" and add a numeric column named "aNumber" as shown in the following.
The list needs to be named "Test" with a column named "aNumber" because these values are hard-coded in the source code. In the next version we will extend this Web Part to make the list name and the column(s) to sum as input values.
Add the list to the site.
Once you add the list to the site, add a few new items in the list.
Deploy Assembly in Sharepoint Bin Dir
From IIS locate the home directory of your MOSS deployment.
Copy the assembly (myWebPartForBlog.dll) of the previously built class library to the bin directory of the MOSS deployment. - which in my case is the folder - C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin.
(Create the bin directory if it does not exist).
Edit SharePoint Web.Config
Now go and edit the web.config file for this site, which will be located in the root folder for this site. - in my case it is located at C:\Inetpub\wwwroot\wss\VirtualDirectories\80.
In the SafeControls section of the web.config file add the new web part as a safe control by adding the line:
The assembly is the name of the compiled dll (without the .dll), the namespace is the namespace used in the source file, which in my case is the same.
This is an important step, I have found this to be a sensitive area, even the slightest difference in this line will stop the web part from appearing in the web site - the tags, assembly name, namespace are all case sensitive.
Once you have edited the web.config file, go into your MOSS site and edit the settings, by clicking on "Site Actions" > "Site Settings" and select "Web Parts" from the Galleries area.
You will see a big list of all the Web Parts that are available to your sites, click on the "New" link to add our custom web part.
Once you have clicked on "New" you will see another list of Web Parts to choose form, scroll down and locate and select "myWebPartForBlog.SumListColum" and select "Populate" at the top of the screen.
If at this point you cannot see the newly created Web Part, ensure you have entered the information in the web.config file correctly. - check all case sensitivity, spelling, etc.
Now you might think your Web Part is available, try and add it to your site.
- You will receive a security exception - an error similar to the following.
This error is due to the fact that our classs library is making a reference to the Microsoft.SharePoint namespace, which requires a higher level of trust than is normally available in a minimum trust environment (default).
This presents two issues: a) our site on which we have dropped this web part is no longer available because we have not handled the security exception, b) we need to resolve the security exception so our web part will function.
There is an excellent article that describes this better than I can, read it here.
As described in the artcile there are a number of approaches to resolve the security issue. In my opinion the best approach is to leave the assembly in the bin directory and write a custom security policy for our assembly to allow it to be able to access the resources it needs to function (which in our case is the Microsoft.Sharepoint namespace and any related assemblies).
However, for the purposes of this article, the simplest approach is to deploy the assembly into the GAC - which in some cases may in itself be a breach of securiy, as this means the assembly is made available to a wider group of applications (as it will be in the GAC).
In a later version of this assembly, I will write a version that uses the custom security policy approach (Watch this space).
On the error page (above), you will see a link to the "Web Parts Maintenance Page" for the current site, you can go into this page and close the "bad" web part so atleast your site is available to users while we resolve ths issue.
Fix Security Issue
Lets first sign the assembly so we can deploy it into the GAC.
Open up the project (if not already open) in visual studio 2005.
From visual studio, select the project properties from the "Project" menu, you will be presented with the following screen.
On the left hand side, notice the signing tab, click it to select it, and on the next screen, select "sign the assembly" and from the "Choose a strong name key file" drop down choose "New
When you select "New", enter a file name for your key file on the next popup screen as in the following and click OK to continue.
You should see a new key file in your solution explorer, build your project and re-deploy to the bin directory of your sharepoint deployment location as described above.
Now we need to add our assembly to the global assembly cache (GAC) as all assemblies in the GAC run under full trust - which is what the reference Microsoft.SharePoint needs. Only administrators can add assemblies into the GAC - hence why assemblies in the GAC are considered safe and are provided full trust.
On your MOSS Server, locate the gacuil.exe for .Net 2.0, in my case it is in the c:\windows\system32\dllcache directory and enter the following command from a dos prompt. (dos prompt can be accessed from start>run> then enter "cmd" and press OK.
gacutil.exe /i [location of your assembly]\myWebPartForBlog.dll
Now we have to remove the previously added Web Part and re-add it so that the new version can be correctly referenced in the Web.Config file.
From the top level site, go into "Site Actions">"Site Settings">"Modify All Settings"
Select "Web Parts" from the Galleries area.
Scroll down and find our custom web part, "SumListColumn.webpart", click on the edit icon, and then select "Delete Item" when the item properties popup.
Now lets find our assembly in the GAC, lets get a list of all the assemblies that have been added into the GAC and copy this into a text file, use the following command form the dos prompt.
gacutil.exe /l > assemblies.txt
Now open this text file by issuing the following command from the dos prompt
notepad.exe assemblies.txt
Search for our assembly by searching on its name, myWebPartForBlog.
Highlight the entire line when found and copy it as shown in the following.
Open the SharePoint web.config file and change the previously added line with the following
Now go into the site settings of your top level site and re-add the web part to the web part galleris area as described above.
Notice this time when you add the new item and scroll down the list of available web parts to add, you will see a value in PublicToken - before this was null.
Select this web part, and click on populate above.
Test Web Part
Now, go to the test site - the same site in which we previously created the "Test" List, make sure you add a few rows and then drop the web part under this list and you should see a total for the numeric column, as show in the following.
Go into the settings of the web part (Edit) and make the direction "Right to Left" to correct the alignment.
Thats It!
Hope you enjoyed it,
Feel free to leave any comments.
No comments:
Post a Comment