Monday, January 9, 2012

Display Lists in the SharePoint site

I am going to explain my experiences while first time creating a console application in SharePoint 2010. It will be helpful for the beginners. I found few differences while creating applications in sharepoint 2010.

To know all the things what I have got please go through the below steps.

  1. Create a web application using CA(Central Administration).
  2. Add site collection and check whether it is working or not.
  3. In my example I have created an application in cloudshare (for more information visit my cloudshare post)
  4. I have created a site “http://sp2010:60000”. I have tested it is working fine
  5. Now I want to write a console application to list out all the lists in the sharepoint site which i have created.
  6. To create an application we need a visual studio 2010 only, below versions do not work for 2010 development.
  7. Open Visual Studio 2010
  8. Select ‘Visual C#’ and ‘Console Application’ template as below
    image
  9. Enter Name and Click OK.
  10. Now console template will be appeared with the syntax code.
  11. Add reference “Microsoft.SharePoint.dll” by browsing the root folder of the sharepoint.(%systemdrive%\Program Files\Common Files\Microsoft Shared\\Web Server Extensions\14\ISAPI)
  12. It will add SharePoint assembly to the application.
  13. Now we can access the SharePoint objects in out coding.
  14. Add name space “using Microsoft.SharePoint”;
  15. Write code as below showing
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.SharePoint;

    namespace ShowLists
    {
    class Program
    {
    static void Main(string[] args)
    {
    SPSite site = new SPSite("");
    SPWeb web = site.OpenWeb();
    foreach (SPList list in web.Lists)
    {
    Console.WriteLine(list.Title);
    }
    }
    }
    }

  16. Now execute the program.
  17. It would show an error as below, Please notice we have already added the Microsoft.SharePoint.dll file reference, even though it is showing an due to incompatibility with the .netframework which I have been selected(.netframework4.0).
    image
  18. To rid this error please change the .netframework be following the below steps
  19. Go to the Project properties and change the .netframework version at Target Framework
    image
  20. Change it to “.Net Framework 3.5”
  21. and build the program. It will build successfully.
  22. Now try to Execute the program by Run it.
  23. Again it would get an error as below
    image
  24. I have checked earlier as it is existed but not found. This error is occurred because of that application is targeted to 32bit(x86) environment. Microsoft SharePoint 2010 does support 64bit(x64) only.
  25. So please change that target machine type as below, by clicking on the Build tab of project properties.
    image
  26. Now Run the program. It will execute with the lists.

No comments:

Post a Comment