Monday, November 8, 2010

Type mismatch Error Trying To SET the Property: Enable32BitAppOnWin64

Following command will help to enable the IIS 6 for 32 bit applications.

Open a command prompt and navigate to the %systemdrive%\Inetpub\AdminScripts directory.

Type the following command:

cscript adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 True

you can also use following

cscript adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 1


to revert

cscript adsutil.vbs set W3SVC/AppPools/Enable32BitAppOnWin64 0

Friday, November 5, 2010

Sharepoint 2007 - Attaching Event Receiver to specific List that updates SQL Server Table

In the following post , i am trying to explain how can we can develop and attach a event receiver to a specific sharepoint custom list.This event receiver is updating a SQL server table.


SharePoint Lists & Libraries Overview:
A list is a collection of information that we can share with team members.
A SharePoint list is a collection of similar items. A list contains columns or fields that define the item data or metadata schema. Each item stored in a list shares the same schema. Technically lists also include libraries, but libraries are often seen as separate from lists or at least specialized forms of lists. In lists items are defined by metadata or the columns of a list with documents being attached to that metadata. In a library a document is the item with library metadata supporting the document.
Lists in SharePoint resemble database tables in structure and behavior. Lists support various field or data types, and can have triggers that react to list events such as creating, updating or deleting items. In addition lists can be configured to filter, sort or group items based on item data or properties.
SharePoint lists also support various methods of visualization, both in the display of data and in the editing or entry of item data.
Lists in SharePoint are based on list templates, such as document libraries, calendars, contact lists, picture libraries, and others, that define the schema for new lists. We can create multiple lists based on a single list template.
We can also attach workflows to lists, allowing more complex behavior of lists and libraries.
MOSS offered developers the ability to catch certain user actions in code about SharePoint Lists & Libraries and react programmatically. These user actions triggered a set of asynchronous events which happened after the user had completed the action.
We can categorize SharePoint events in two different categories: by the “level” which fires the event (site, list, item), and by the type of the event (synchronous and asynchronous).

• Synchronous: happens 'before' the actual event, we have the HttpContext and we can show an error message in the browser and cancel the event.
• Asynchronous: happens 'after' the actual event, there's no HttpContext and we cannot directly show an error message or cancel the event – we can handle what happens after the event is fired.
As an example of synchronous and asynchronous events we can take item-level events “ItemAdding” and “ItemAdded” – by handling of “ItemAdding” we can take a look what that item is, and, if necessary, cancel the adding and display the error message in the SharePoint page to the user. When handling “ItemAdded”, we know that the item is already in the SharePoint list, but we can start some post-add actions with that item.


Creating Custom Lists:

Following steps are for creating custom list using sharepoint outofbox features.
You can create the same using feature and CAML.

Step 1: To create a new Custom List first go to Site Settings page and select Site libraries and lists under Site Administration.




Step 2: Choose create a new Content option from Site Libraries and Lists page.



Step 3: Choose Custom List option under Custom Lists to create a new Custom List/Library from Site Settings.





Step 4: This Screen is for New List Creation by providing List Name and Description fields and after that Select Create button.




Step 5: The new List is created with MyList name which displays in Quick Launch bar and in Navigation bar also.






Step 6: We have to create columns to MyList list by selecting Create Column field from list Settings.



Step 7: The below screen displays the Column Name and Column Description fields including the type of column information. We have the option of Maximum Number of Options of that column and Default value also. After providing the Column Name and Column Description click on Ok button.
In the same way we can add multiple columns to the List.


Step 8: After adding columns to List we can enter the values to specific fields. For that we need to select NewItem to add a NewItem to List.




Step 9: The below screen displays all the columns list and this Item will be added to List by clicking OK button after entering all field values.




Step 10: The below image displays all the Items of that List







Creation of Event Receiver:

We will be creating Event receiver forr the list we just created above.
We want whenever anything gets added/updated/deleted, at the same time one of the SQL table be updated accordingly.
We would be creating two features
1) for Event Receiver
2) for Activiating and Attaching the event receiver to this specicific list.
The second feature is also important, in absense of this you would end up attaching this event receiver to all the lists.

Step 1: Create a New Visual Studio Project to implement an Event Receiver. Choose File >> New Project to create a New Project.



Step 2: Select WSPBuilder Project option from Visual Studio Project Templates list and select OK button.
Note: You just have WSPBuilder installed in your environment.That is the great tool to work with Sharepoint.You can download that from http://wspbuilder.codeplex.com/


When you've created the project, you'll see a structure like this one in your solution explorer:
The WSPBuilder will create the 12-folder. It will also add the file “solutionid.txt” which contains a GUID to be used on the .wsp package, for easy reference. We will also get a strong-key generated.

Step 3: Right click on the project and choose Add – New Item



Step 4: Choose WSPBuilder node from Categories list and select Event Handler from Templates list and enter a Proper Name in Name text field. Select Add button to proceed.





Step 5: With WSPBuilder, when we create a new item based on a template, we'll get a dialog asking for some variables - and since this is a feature, it's going to need a Title, Description and Scope:





Step 6: The solution tree will be populated with a few new things, in this case the MyList that we chose to create. Implement the functionality of overriding all the required events like ItemAdded, ItemAdding, ItemUpdated, ItemUpdating, ItemDeleted, and ItemDeleting etc in MyList.cs file.

Add a Config file to the project if we want to maintain any key elements like DB Connection strings or file paths etc.




Following screens are the mylist.cs code sample



Note: Insertdb, updatedb,deletedb are the seperate methods that will accept the parameter and perform the db operations.

Class/Functions in EventHandler: (MyList.cs)
• MyList: This is an EventHandler Class which needs to implement SPItemEventReceiver class comes from Microsoft.SharePoint namespace.
• ItemAdded: This is one of the methods belongs to SPItemEventReceiver base class to override. This event is being handled after the action occurs. We must override this method and this method uses ListItem property to return an object that represents the new list item, and then modifies the body text of the item. This event uses InsertDB function which is implemented in DBFunctions class to implement the records insertion into database.
• ItemDeleting: This indicates that the event is being handled before the action occurs. In this event we created one DBFunctions class object and access DeleteDB function which is implemented to delete an item based on.
• ItemUpdated: suffix “-ed” indicates that this event is being handled after the action occurs. So this event is being handled once the updation occurs. This event uses SPListItem class which represents an item, or a row in List. We accessed UpdateDB function which is implemented in DBFunctions class belongs to MyList namespace to update all the items based on ID.



Class/Functions in DBFunctions: (DBFunctions.cs)

• DBFunctions (): This is a constructor which invokes immediately when an object is instantiated for DBFunctions class. Collected all the key elements from web.Config file like connection string, and xml file path.
• InsertDB: This function accepts Id, Title, Contact, PhoneNo input parameters and inserts all these into tbl_MyList table. For this, we used SqlCommand ADO.Net object to add all parameters and execute the insert query.
• UpdateDB: This function accepts Id, Title, Contact, PhoneNo input parameters and updates all these parameters into tbl_MyList table based on ID. We used SqlCommand object to execute the Update query.
• DeleteDB: This function accepts only Id as input parameter to delete the record from tbl_MyList based on Id.






Step 7: Add a New Project for Event Handler by right clicking on the Solution Explorer and selecting Add >> New Project.



Step 8: When we create the project, we'll see a structure like this one in the solution explorer:




Step 9:Right click on the project and choose Add – New Item.






Step 10: Choose WSPBuilder node from Categories list and select Feature with Receiver from Templates list and enter a Proper Name in Name text field. Select Add button to proceed.





Screen 11: When we create a new item based on a template, we'll get a dialog asking for some variables - and since this is a feature, it's going to need a Title, Description and Scope:



After creation of this project for Event Handler, we can override different events related to Feature such as FeatureActivated, FeatureDeactivating, FeatureInstalled, and FeatureUninstalling.



Class/Functions in Feature with Receiver: (MyListReceiver.cs)

• MyListReceiver: This is the class which overrides all Feature events such as FeatureActivated, FeatureDeactivating, FeatureInstalled, FeatureUninstalling. This class extends the SPFeatureReceiver class from Microsoft.SharePoint namespace.
• FeatureActivated: This event fires when the feature is activated. In this method we accessed the List and adding all the Event Receivers to that list which are implemented in EventHandler MyList.cs file.

Note: When activated this feature, this will enable the eventreceiver events for the mylist only.

After building these two projects and wsp as well, we need to deploy these two wsp files in server. We can deploy these wsp files using batch files in server. Once after deploying the wsp files, we can check the wsp in Central Administration whether the wsp is deployed into site or not.

Step 12: This is all the Deployed/Not Deployed wsp files list in Central Administration.





Step 13: After deploying MyList Event Handler wsp, the below image shows that as Not Deployed.


Step 14: We can manually Deploy the wsp by selecting it and it goes to Solutions Properties page. By selecting Deploy Solution option we can deploy it.








Step 15: Once after selecting Deploy Solution option it displays the Solution information in Deploy Solution page and some options to choose when to deploy the solution. After choosing these options, we can select OK button to deploy the wsp.






Step 16: We can see the Deployed wsp (mylist.wsp) from below diagram after Deployment process is over.




Step 17: After this process we need to Activate the Feature from Site Settings by selecting Site features under Site Administration.



Step 18: Once we select Site feature option all the Activated/Deactivated features will be displayed in Site Features page like the below image. So by selecting Activate button we need to activate the feature.







Step 19: The below image is after Activating the Feature, the Activate button turns to Deactivate.




We need to deploy the Receiver wsp also using batch file after activating the Event Handler feature. Once it is deployed into website, we need to Activate that feature also same as above screens.

Step 20: Once everything is done, we can test the list by Adding new Item into MyList list.





Step 21: We can find all the entered values related to that list in the table after clicking the OK button.






Step 22: We can edit the list item by right clicking on the Item and selecting EditItem option.





Step 23: We can change the values after editing the Item.






Step 24: We can find all the updated values in the table also.




Step 25: We can Delete the Record by right clicking on the item and by selecting DeleteItem option.






Step 26: Before deleting the item it displays one Confirm window.








Class/Function used and their purpose and Input and expected output


Deployment Process:
Collect all wsp and dll files from MyList EvntHandler and MyListReceiver Event Receiver to deploy into server.

Create a batch files (file with .bat as an extention) for first wsp file with the below commands.

@set PATH=C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN;%PATH%

stsadm -o retractsolution -name MyList.wsp -immediate -allcontenturls
stsadm -o execadmsvcjobs
stsadm -o deletesolution -name MyList.wsp -override
stsadm -o execadmsvcjobs
stsadm -o addsolution -filename MyList.wsp
stsadm -o execadmsvcjobs
stsadm -o deploysolution -name MyList.wsp -immediate -allowGacDeployment -local
stsadm -o execadmsvcjobs

Create another batch file (file with .bat as an extention) for receiver wsp file with the below commands.

@set PATH=C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN;%PATH%

stsadm -o retractsolution -name MyListReceiver.wsp -immediate -allcontenturls
stsadm -o execadmsvcjobs
stsadm -o deletesolution -name MyListReceiver.wsp -override
stsadm -o execadmsvcjobs
stsadm -o addsolution -filename MyListReceiver.wsp
stsadm -o execadmsvcjobs
stsadm -o deploysolution -name MyListReceiver.wsp -immediate -allowGacDeployment -local
stsadm -o execadmsvcjobs


DataBase:
Tables Used
1 Tbl_MyList

File(s) Need to deploy:
1. MyList.wsp
2. MyListReceiver.wsp