Monday, November 29, 2010

Creating & Adding an Event Handler

In this document, we will create an event handler that captures the events of the document library and add an entry in the cell of the document added. We can add an event handler using code or as a feature. In order to write an event handler, we will first create a sharepoint site and create a document library. We will, then, capture the events of the document library.




b1-1



1 Writing an Event Handler


We will start from the Visual Studio 2005 new project window. In the new project window, select C# Window application, then, class library as encircled in the figure below and name the project “DocLibEventHandler”.




2.jpg



1.1 Adding Reference


The first thing we will do in the class library is to add reference of the name space Microsoft.SharePoint. This is done by right clicking on the References link in the solution explorer and then clicking on “Add Reference…” as encircled in the following figure:



3.jpg


Clicking on add reference will generate a new pop up window Add Reference. We will select the Windows® SharePoint® Services namespace, as encircled in the figure below, and click on OK. It will add the Windows® SharePoint® Services namespace in our project.



4.jpg




We will add the names spaces, encircled red in the figure below, in our project so that we can directly use the classes in that namespace without specifying the complete path.






5.jpg




1.2 Changing Class Name


We will again go to the solution explorer and change the name our class from “Class1” to “DocLibEventHandlerClass”. This can be done by clicking on the class name in the solution explorer and right click on the class name. This will open a new window from which we will select Rename and change the class name from “Class1” to “DocLibEventHandlerClass”. As you change the name of the class, a prompt will appear, shown in the figure below, and ask would you like to rename all references to the code element “Class1”? Click on “Yes”



6.jpg



1.3 Inheriting Class


We will inherit our class from the class SPItemEventReceiver. The path to the class SPItemEventReceiver is Microsoft.SharePoint.SPItemeventReceiver. We have already added the namespace Microsoft.SharePoint, therefore, we just writet the name of the class SPItemEventReceiver for inheritance as shown in the figure below:



5.jpg



1.4 Capturing Events


MOSS provides two types of events namely, synchronous and asynchronous. The “…ing” event occurs before the action starts and the “…ed” occurs after the actions ends. “…ing” events occur synchronously while the “…ed” events occur asynchronously.


8.jpg


Synchronous events



  • Occur before the event.

  • Block the flow of code execution until your event handler completes.

  • Provide you with the ability to cancel the events resulting in no after event (“…ed”) being fired.


Asynchronous events:



  • Occur after the event.

  • Do not block the flow of code execution in SharePoint


To capture these events, class SPItemEventReceiver provides different methods, as shown in the right side of the above figure, which can be seen through Object Browser by pressing Alt+Ctrl+j to see the list of the methods by browsing through Microsoft.SharePoint to SPItemEventReceiver. We will create a simple program for demonstration purpose. The code will override methods ItemAdding, ItemAdded, ItemUpdated and ItemUpdating in the class SPItemEventReceiver.


1.5 The Code


The code will run when a new file is added in the document library and also when an existing document is updated. It will add an entry in the list “CEO Docs Access Log” describing the nature of the event and date on which event occurred. The code is as under:


namespace DocLibEventHandler


{


public class DocLibEventHandlerClass : SPItemEventReceiver


{


public override void ItemAdded(SPItemEventProperties properties)


{


SPListItem doc = properties.ListItem;


doc["Comments"] = “Document has been added”;


doc.Update();


}


//you can use following methods as well


public override void ItemAdding(SPItemEventProperties properties) { }


public override void ItemUpdated(SPItemEventProperties properties) { }


public override void ItemUpdating(SPItemEventProperties properties) { }


}


1.6 Signing the Assembly


The next step is of signing the assembly. To do this, you have to move to the properties window of the project. To do this, right click on the project name “DocLibEventHandler” in the solution explorer and click on the properties in the popup menu. This will open the properties window, select the Signing Pane of the properties window and check the “Sign the Assembly” option. Select <New…> from the “Choose a strong name key file:” and give the name that you desired. In our case, we named it “DocLibeventAssembly” as shown in the figure below:



9.jpg




1.6 Build Project


We will build by pressing F6 or clicking on Build in the tolls menu and then clicking on Build Solution. Build will generate the file “DocLibEventHandler.dll” in the debug directory.


1.7 Copying into GAC


The file DocLibeventHandler.dll can be found in the debug folder whose path is C:\…..\Visual Studio 2005\DocLibEventHandler\DocLibEventHandler\bin\Debug. Depending upon your installation, you can find the file in the debug folder. Copy the file “DocLibeventHandler.dll” from this folder and paste it in the GAC folder which is normally found at C:\WINDOWS\assembly. After copying the file in the GAC, copy the public token key as encircled in the figure below:



10.jpg



There are many other ways of adding assembly in GAC.



1.8 Registering the Event Handler


In order to register the event handler, add a new console project in the solution by right clicking on the solution “DocLibEventHandler” in the solution explorer as shown in the figure below:



11.jpg



Select a console project in the “Add New Project” window and name it “DocLibRegApp” as shown in the figure below:



2.jpg



Add the following code in the main method.


namespace DocLinRegApp{


class Program {


static void Main(string[] args) {


SPSite sp = new SPSite(“http://servername”);


SPWeb website = sp.OpenWeb();


SPList DocLib = website.Lists["DLOne"];


string assm = “DocLibEventHandler, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1d6b41a3ed9a92cb”;


string class = “DocLibEventHandler.DocLibEventHandlerClass”;


DocLib.EventReceivers.Add(SPEventReceiverType.ItemAdded, assm, class);


DocLib.EventReceivers.Add(SPEventReceiverType.ItemAdding, assm, class);


DocLib.EventReceivers.Add(SPEventReceiverType.ItemUpdating, assm, class); DocLib.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assm, class);


}


}


}


The string assm describes the assembly details that we have added in the GAC and class is the name of our class. Press F5 to run the project.


1.9 Changes in the Document Library


Open the site and add document in the document library. Comments column will have the value “Document has been added” as shown in the figure below:





13.jpg


SharePoint 2007 - Orphan Sites

I was working with alongside one of the guy's from Microsoft the other day and we were implementing the MS IT Site Delete Capture Utility available from Codeplex. Part of the testing required me to create/delete & restore sites. Briefly, the site delete capture utility intercepts both site/web delete requests and archives the site/web. Having deleted the site from within Site Settings of the site. I then attempted to restore the site using STSADM at which point I was presented with an message telling me that the site collection already existed and that I should use another name. Ive just deleted it so that cant be right, so i immediately navigate to Central Administration and surprisingly the offending site is still there. I then try to delete the site from within Central Administration page but again it would not delete. Finally, I try to delete the site with STSADM but I got this error with stsadm.exe -o deletesite: The system cannot find the path specified. (Exception from HRESULT: 0x80040E2F). Not sure at this point if this is as a result of implementing the site delete feature, I look at the Event logs, ULS and the log that is provided with the site delete feature. Nothing indicated that the site delete feature was at fault.

A quick search and I learn of a term called "Orphan Sites". As it happens, orphan sites are not that uncommon and Microsoft had previously worked on a fix. It seems however that in some cases this issue can still occur but what Microsoft have done is provide some pretty cool STSADM operators to help resolve it, if it does occur.

An Orphaned Site is where SharePoint only has partial information and not a complete set of data for a given site collection in your Windows SharePoint Services or SharePoint Portal Server content databases or configuration databases. The site may in fact still be viewable via the browser, but you may notice that many things are broken.

An orphaned site may manifest itself in one of the following:

  • STSADM -o restore fails to restore the site, even with the -overwrite option when you know the URL exists.
  • You may receive an error code 0x80040E2F. 0x80040E2F means “Violation of PRIMARY KEY constraint”, this equates to STSADM already finding previous rows in the database for the items it's trying to restore.

    NOTE: STSADM first looks at the Configration and Content Databases to ensure that the site does not already exist, if it does and you didn't use the -overwrite switch, it will fail. If STSADM does not find any information, it begins restoring which then results in the problem noted above.

  • STSADM -o deletesite fails to delete the site
  • You may receive and error which states "The site does not exist". Again, No site information in Configuration or Content database in their respective Sites tables. Thus the other remnants of the site cannot be cleaned up.

To resolve this issue you must carry our the following 2 actions:

STEP 1

  1. Open a command prompt and go to change directory to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN and type the following:

    stsadm -o databaserepair -url http://Web/sites/Site -databasename <DatabaseName> (This will list all sites that are orphaned)

  2. Next, type the following:

    stsadm -o databaserepair -url http://Web/sites/Site -databasename <DatabaseName> -deletecorruption

  3. You will then be presented with a result in xml format with a deleted attribute that equals to YES

    <OrphanedObjects Count="22"><Orphan Type="SPSite" Id="{E732BEEC-F4BD-40D7-A51F-7B33BA7D5698}" Deleted="Yes" />

STEP 2
  1. Launch SharePoint Central Administration.
  2. Select Application Management and under SharePoint Web Application Management select Content Databases
  3. Select the Content Database where the offending site remains.
  4. Make a note of the following settings:

    • The name of the Content Database.
    • The number of sites before a warning is sent.
    • The maximum number of sites setting.
    • The Windows SharePoint Services Search Server.

  5. Click to select the Remove content database check box, and then click OK. When you are prompted to confirm that you want to remove the content database, click OK.
  6. Click OK.
  7. On the Manage Content Databases page click Add a content database.
  8. On the Add a Content Database page, specify the database server, the database name, and the database capacity settings. Then, click OK.
  9. Once you have dont that, select the Content Database you have just re-added and ensure you select the Windows SharePoint Services Search Server noted earlier.
Having done that you should now be able to restore/recreate a site with the old name.

For further reading try the following Sharepoint Orphans Explained

Friday, November 26, 2010

Server error: http://go.microsoft.com/fwlink?LinkID=96177

While trying to install WSS 3.0 SP1 (http://technet.microsoft.com/en-us/office/sharepointserver/bb735839.aspx) the installation get stuck at task 9 of 10 of SharePoint Products and Technology Configuration Wizard. When tried to access the server,
encountered the following error:

Server error: http://go.microsoft.com/fwlink?LinkID=96177

When look into the “Event Viewer”, found the following error description:

The schema version (3.0.149.0) of the database DATADATABASE_NAME on DATABA_SESERVER_NAME is not consistent with the expected database schema version (3.X.X.X) on DATABASE_NAME. Connections to this database from this server have been blocked to avoid data loss. Upgrade the web front end or the content database to ensure that these versions match.

Resolution:

The first thing we did was either remove content database from the web application which are having the error or Detach the database. This doesn’t seem to work. So we run the command

stsadm –o upgrade –inplace –url Central_Administration_URL –forceupgrade

and we were back in the business J.

Monday, November 22, 2010

An unexpected error has occurred. Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove

"An unexpected error has occurred. Web Parts Maintenance Page: If you have permission, you can use this page to temporarily close Web Parts or remove personal settings. For more information, contact your site administrator. Troubleshoot issues with Windows SharePoint Services."

You may receive an error with a SharePoint page that has other web parts. The immediate fix is to delete the web part, but that's not a real workaround. Changing the trust level for the site worked for me.

This is the complete fix:
1. Open web.config file of current web application
2. Find the node that begins 3. Change the trust level from WSS_Minimal to WSS_Medium
4. Save the file
5. Drop to command prompt and execute : iisreset

Failed to instantiate file "TopAnswer.webpart" from module "WebPartPopulation": Source path "dwp\TopAnswer.webpart" not found.

Issue Details:

Failed to instantiate file "TopAnswer.webpart" from module "WebPartPopulation": Source path "dwp\TopAnswer.webpart" not found.
Troubleshoot issues with Windows SharePoint Services.

Solution Details:

1. Clear the SharePoint Fonfiguration Log Files

2. Upgrade MOSS SP2.

Monday, November 15, 2010

MOSS 2007, CSS, and you, the Non-Developer – the GlobalTitleArea










we started our little journey by changing the look and feel of the Quicklaunch menu and column by making changes in our copy of the CORE.CSS file.

We changed the background colors of the quicklaunch menu header and item cells, the colors of the default links and the borders of the quicklaunch menu. Today we are

going to focus on the area of the page that runs from the cell directly above the quicklaunch, actually part of the pagemargin, to the top of the global

bread crumb links at the very top of the page.



Before we get started I would like to stress the following:



  1. We are making the changes documented in this article in a COPY of the CORE.CSS file and then applying those changes through the administrative UI. You can rename the copy to most

    anything you want, I am using My_Core.css.


  2. I am working in a virtual environment in order to minimize any risk.


  3. Most administrators will not have access to the server. If that is your case contact

    your server administration team and request a copy of the CORE.CSS file and test any changes you make in a test environment prior to implementing them in any

    way, shape or form.



Once again we will focus on changing background and font colors. In addition to changing

the background colors by setting the background-color class to a color (#ffccff for example) we will look at how we can change the way some of those areas

appear by adding our own images.



  1. We’ll get started by changing the color of ms-titleareaframe, which will change

    the color of the cell that runs the width of the page directly above the quicklaunch menu and page body.





  1. In the next step we will add a background color to the cell between the globalnavigation tabs and the site actions menu. In order to this we’ll

    have to add a background-color declaration to the .ms-banner class and assign that declaration a color.




  1. After adding the background color to the ms-banner class above you will see small cells on the left hand side of the global navigation tabs and the right

    hand side of the site actions menu. In order to set a background color on those cells we will have to remove the image that Microsoft uses for the background

    and then change the property of the background-color declaration.




  1. Next we will set the background color of the cell that runs from the left of the site title over to the search scope box. We will do this by changing the

    property of the background-color declaration as shown in the following screenshot.




  1. Now if you look at the previous screenshot you’ll see that the ms-sitetitle class does not set the color of the entire row here. There will be a

    white background behind the sitetitle icon on the left hand side of the page header as well as a white background around the search scope drop down menu and

    search box on the right. We set the background color in both places by removing the default Microsoft image and then changing the background-color

    declaration in the ms-globaltitlearea class.




  1. At this point the last background area we have to change is the cell running the width of the page at the very top that contains the global breadcrumbs

    and the “Welcome User”, “My Site” and “My Links” links. This background is managed in the .ms-globalbreadcrumb class and

    can be changed by changing the background-color declaration.




  1. Now we’ll go back and change the font colors of the links in the header area of our page. It should be noted that in addition to changing the font

    colors here you can also change font-weight, font-family, font size etc… We’ll start with the global links which includes the breadcrumb links in the

    upper left of the page and the “My Site” link in the upper right. We’ll change those font colors in the .ms-globallinks class by changing

    the color declaration.




  1. As you can see above the font color of the “Welcome User” and “My Links” links did not change. For whatever reason SharePoint

    manages those font colors in the .ms-SPLink class and again by changing the color declaration.




  1. Next we’ll change the font color of the site title link. This color is managed in the .ms-sitetitle class and is changed by making a change to the

    color declaration.




  1. Our last font color change will be the color of the “Advanced Search” link. This is done in the .ms-sblink class and as with the other

    changes we have made done by changing the color declaration.




  1. As I mentioned in the opening we are also going to look at how we can change backgrounds using an image. For this I created a .jpg image in Photoshop

    that measures 200px wide and 40px high. I saved that image file in the /_layouts/images/ folder within the 12 hive. In order to display that image as a

    background all we need to do is either replace one of the images we removed in the steps above with the path to our new image or as I have done here add a

    background-image declaration to one of our classes and point that declaration to our new image as shown below.



I think that it’s important to note that it appears to me that the image will resize itself in most cases to fit the

height and width of the cell it is being added to. There may be cases where you will need to add a height or width declaration in order to accommodate the

image you are trying to add. Additionally if your image is much wider or higher than the cell you are inserting it into you can try adding an overflow

declaration and set the property to “hidden” in order for your image to fit properly. From what I have seen it’s more a trial and error

thing to figure out.



  1. The last step we are going to take in this article involves some behavior I find to be a bit strange and don’t entirely understand. I am still

    looking into it but here’s the fix I used for this specific issue. After I made the changes to the .ms-titleframearea we identified above I started

    seeing a large blue box on all my list pages. I found that the box is apparently another Microsoft image and I was able to get rid of it by removing the

    image in the .ms-pagetitleareaframe class as you will see in the following screenshot.



And now a couple of shots of your updated page to date:




Well, that’s all for this article. Next time we’ll look at changing the remainder of the pagemargin around the main page body as well as

changing the font colors of some of our default web parts. I hope you found this useful. If you have any questions or comments please don’t hesitate to

post them!


Until next time…




Sunday, November 14, 2010

WSS Web Services Web Reference

WSS Web ServicesWeb Reference
Administration Servicehttp:///_vti_adm/admin.asmx
Alerts Servicehttp:///_vti_bin/alerts.asmx
Document Workspace Servicehttp:///_vti_bin/dws.asmx
Forms Servicehttp:///_vti_bin/forms.asmx
Imaging Servicehttp:///_vti_bin/imaging.asmx
List Data Retrieval Servicehttp:///_vti_bin/dspsts.asmx
Lists Servicehttp:///_vti_bin/lists.asmx
Meetings Servicehttp:///_vti_bin/meetings.asmx
Permissions Servicehttp:///_vti_bin/permissions.asmx
Site Data Servicehttp:///_vti_bin/sitedata.asmx
Site Servicehttp:///_vti_bin/sites.asmx
Users and Groups Servicehttp:///_vti_bin/usergroup.asmx
Versions Servicehttp:///_vti_bin/versions.asmx
Views Servicehttp:///_vti_bin/views.asmx
Web Part Pages Servicehttp:///_vti_bin/webpartpages.asmx
Webs Servicehttp:///_vti_bin/webs.asmx