Friday, January 7, 2011

Referring Dll in Sharepoint 2007

Referring Dll in Sharepoint 2007 to add feature in Sharepoint.

Below article will showcase “How to restrict user to delete item from Sharepoint Library”. When user tries to delete any item, we will navigate them to Error page displaying custom message.
Restricting delete operation is a kind of feature we will add in Sharepoint. This feature will get deploy in via Feature.xml file and Elements.xml file.
What is Feature.xml?
This xml file will allow developer to deploy out-of-box feature onto Sharepoint 2007. This xml will allow performing the deployment without any coding. Feature.xml will define base property for a feature. It even reference all elements defined in sharepoint  Any feature can be turn on/off by activating and de-activating feature via sharepoint commands. Hence, feature.xml file can be used extensively in Sharepoint.
Note: Please perform below steps on server where sharepoint is deployed and running.
Below .Net code will help us to detect event during delete operation and help us to display custom error message. Open your new C# project and add below code to your class file.
namespace ItemDeletingHandler
{
    public class ItemDelete:SPItemEventReceiver   
    {
        public override void ItemDeleting(SPItemEventProperties properties)
        {
            properties.Cancel = true;
            properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";
        }
    }
}
Here, you need to inherit from SPItemEventReceiver inbuilt class which will help you to detect event when some action occurs on your document library. You need to override ItemDeleting method. Build your project for any compile errors.
Signing DLL with strong key:
We need to refer DLL to Sharepoint. For this we need to GAC DLL. For gacing, DLL must have strong name. VS 2005 help to sign DLL with strong name. Follow below steps in VS 2005:
Move to Project -> “Project Name” Properties -> Signing tab,






















Deploying DLL in GAC.
Now, you can Build your solution: Build -> Build Solution. Now, your DLL is sign with strong key. Go to your project debug/Release folder and Drag and drop your DLL to GAC (C:\Windows\assembly).

Here, everything related to coding is completed. Now, we have to deal with XML files (to add features) and Command prompt (to activate features).

Creating project name folder in server
Every sharepoint features are found in Sharepoint Features folder (C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\FEATURES). As we are activating sharepoint feature manually, so we need to create our feature folder in this folder, so sharepoint command’s can refer it during activation. Follow below steps:
1.       Navigate to above folder.
2.       Create folder with .Net solution name (in our case its ‘ItemDeletingHandler’)
3.       Create Feature.xml and Elements.xml file.
4.       Refer below section to add content in Feature.xml and Elements.xml file.

Feature.xml
Feature.xml file will include base properties for feature. This xml file will also refer few supporting file like xml, pdf, doc etc. In our case, its Elements.xml file. Add below code to Feature.xml file:
  
<Feature
   Scope="Web"
   Title="Deleting Event Handler"
   Id="7D1AE945-1E28-4224-AC60-B6FCB49DDF5E"
   xmlns="http://schemas.microsoft.com/sharepoint/">
   <ElementManifests>
      <ElementManifest Location="Elements.xml"/>
   </ElementManifests>
</Feature>

Scope: It refer to the scope of feature and within this scope, feature will be bind. Scope could be: “Farm”, “Web application”, “site collection”, “site”. In our case, its Web application, so we can use this feature to any Document Library in this web application.

Title: Title of this feature, as shown in MOSS.

ID: Every feature must be identify uniquely by sharepoint. You can generate GUID via VS 2005 – Tools -> Create GUID -> copy or generate new GUID and then copy it to clip board memory.

Xmlns: This is mandatory field require during activating feature in xml.
Element Manifests: It will refer to another xml file which actually gives all information about assembly.

·         Elements.xml
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
   <Receivers ListTemplateId="104">
      <Receiver>
         <Name>DeletingEventHandler</Name>
         <Type>ItemDeleting</Type>
         <SequenceNumber>10000</SequenceNumber>
         <Assembly>ItemDeletingHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5500998af4e34cf5</Assembly>
         <Class>ItemDeletingHandler.ItemDelete</Class>
         <Data></Data>
         <Filter></Filter>
      </Receiver>
   </Receivers>
</Elements>
ListTemplateID: Every elements in sharepoint has given unique IDs. So whatever elements we are targeting for event detecting, we should specify their ID. In our case, we are referring Document library whose id is ‘104’.
Assembly: You can refer assembly name and public key token name from GAC.
Class: It should be Namespace.Classname.

Installing and Activating feature in Sharepoint
We can activate features in feature.xml file with 2 methods: Command prompt and object model. We will only cover Command prompt as of now.
Open command prompt on your server and write below command

  1. stsadm -o installfeature -filename ItemDeletingHandler\Feature.xml
  2. stsadm -o activatefeature -filename ItemDeletingHandler\Feature.xml -url http://Server/Site/Subsite
  3. iisreset
Note: Underline words in command prompt are built in and mandatory. While not underlined words will get changed based on your project name. URL should be your site URL where you will activate this feature.

Now, you can refer to your site. Upload document to your site Document Library and try to delete the document. User should be navigate to Error page with message: “"Deleting items from http://Server/Site/Subsite is not supported."”

Hope this article helps you. Let me know your feedback on this. You can leave your comments for any queries. Cheers


2 comments:

  1. Thank you for the article.

    I have a similar problem whereby I have created a class library project, built it and dropped the .dll file in the c:\windows\assembly folder.

    However I would like to reference that library from a webpart project (and subsequent web part projects) but I cannot see it in the .Net tab of the Add Reference dialog. Should I manually browse to the .dll file to reference it, and will that work when I deploy and activate the webpart in Sharepoint 2010?

    ReplyDelete
  2. Hi,

    Thanks.

    You can either put in GAC and refer it in Visual Studio with Add Reference Dialog box (.NET Tab). Or refere it directly from its project location (Browse Tab).

    ReplyDelete