Here is the plan:
I Create custom web part that will display current user
II Since getting usename involves accessing Microsoft.Sharepoint.dll, we will need to add our custom web part dll to GAC and then add it to sharepoint
III Make changes to our custom web part dll and "refresh" ASP.NET to see that change
Here is How:
Part – I Building WebPart
1) Created Class library and call it Sharepoint.SampleWebPart
2) Created a class called “DisplayMessageWebPart”
3) Put this code in:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;
namespace Sharepoint.SampleWebPart
{
public class DisplayMessageWebPart : Microsoft.SharePoint.WebPartPages.WebPart
{
private string customMessage = "Hello, world!";
[WebBrowsable(true),
WebDescription("Displays a custom message"),
WebDisplayName("Display Message"),
Personalizable(PersonalizationScope.User)]
public string DisplayMessage
{
get { return customMessage; }
set { customMessage = value; }
}
protected override void Render(System.Web.UI.HtmlTextWriter writer)
{
writer.Write(DisplayMessage + "Current User: " + SPContext.Current.Web.CurrentUser);
}
}
}
8) Let sharepoint know about the dll by editing C:\inetpub\wwwroot\wss\VirtualDirectories\{port}\web.config
add another item to SafeControl list
<SafeControl Assembly="Sharepoint.SampleWebPart, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=57cb4249cf411ed0"
Namespace="Sharepoint.AcceptParamsPart" TypeName="*" Safe="True" />
Part –II – Adding it to GAC
1) Use gacutil.exe (google to find it’s location on your server, if server 2008 just copy from dev machine)
gacutil.exe -i
C:\inetpub\wwwroot\wss\VirtualDirectories\{port}\bin\Sharepoint.SampleWebPart.dl
”i” stand for install
“u” for uninstall in case if you need to
2) In sharepoint site navigate to Site Setting –> Web Parts –> New –> Choose your dll –> Click on “Populate Gallery”
3) You are done. You can add it to any page. It is under Miscellaneous category
Part – III - Makes chages and refresh GAC
If you made changes to you dll, just replace it in the bin folder and then go to IIS –> Find your application pool –> right click –> Recycle
You should know see changes affected in your website
No comments:
Post a Comment