smarthead

Wednesday, February 3, 2010

Sharepoint 2007 How to create custom Web part and add it to the GAC. How to refresh dll in the GAC without IIS restart

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);



        }



    }



 



}




4) Sign your .dll


image


 


image


 


5) Get Public key token from dll by using a tool like .Net Reflector


image


 


6) Build you solution and you should see Sharepoint.SampleWebPart.dll in you bin/debug or bin/release folder


 


7) Copy Sharepoint.SampleWebPart.dll to C:\inetpub\wwwroot\wss\VirtualDirectories\{port}\bin



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

Tuesday, September 4, 2007

How to recover you Sony VAIO laptop

Funny thing happen to me. I lost a recovery CD for my Sony VAIO laptop. Windows XP got screw up as always:)....what do I do? I called Sony two times asking a simple question: "Can you please send me a recovery CD?". Sony said they do not have any more CDs and I need to send my laptop to the repair center for 100$ or more. Another option was to buy a retail version of windows.
Finally, after 20 minutes of word-pong I was told that sony allocates 5GB partition on the hard drive for the recovery software!!! Pressing F10 during computer start-up will start the recovery process which will restore windows to its original settings. Whoalya...