Handling exception in SharePoint 2010 object model


The following exception is very frustrating when we start working with SharePoint object model.  Let us investigate how to solve the exception. 

1.gif 

The preceding exception can be encountered in two scenarios 
  1. Trying to use SharePoint 2010 object model in managed application like console or windows application. 
  2. Trying to use SharePoint 2010 object model in WCF Service or Web services. 
Scenario # 1: The above Exception in console or windows application 

Right click on Console/Windows Application project and select properties. 

2.gif 

Click on Application tab and choose the target framework to .Net Framework 3.5.

3.gif 

After changing Target framework to .NET Framework 3.5, click on Build tab.

4.gif
 
Change the Platform target to Any CPU. 

Now when you run the application, you should not get the exception 

Scenario # 2: Exception when using SharePoint 2010 object model in WCF 

If you are getting the exception while using SharePoint object model in WCF or any Web service. You need to perform the following operations.

Host WCF service in IIS.  See the link below for step by step explanation on how to host WCF 4.0 Service in IIS 7.5


Except we need to make sure that the hosted WCF service is sharing the same application pool with SharePoint. 

5.gif 

Select the Application pool to SharePoint-80.
 
Try to apply both when you use SharePoint 2010 object model.  I hope this discussion will help you. 

Up Next
    Ebook Download
    View all
    Learn
    View all