Quickly integrate with Sharepoint 2010


If you need fast access to Sharepoint 2010 through C# you should take a look at the LINQ to Sharepoint and the SPMetal tool. The SPMetal took generates a class library for all or some objects within the Sharepoint object model.

Quick guide:

  1. Open a DOS-prompt on the Sharepoint server
  2. Go to directory ‘C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN’
  3. Type the following command: ‘SPMetal.exe /web:http://mysite /code:SiteEntities.cs’
  4. Add the SiteEntities.cs to you Sharepoint integration project
  5. Create a program for accessing the Sharepoint objects through LINQ

Code example:

public List<Injury> GetInjuries(
             DateTime? fromDate = null, DateTime? toDate = null
)
{
    var lst = new List<Injury>();
    SiteEntitiesDataContext context = 
         new SiteEntitiesDataContext("http://mysite");
    if (context != null)
    {
        List result = (
                from c in context.Injuries
                where c.OnDate >= fromDate && c.OnDate < toDate
                select c).OrderBy(x => x.OnDate).ToList();

        foreach (InjuriesItem inj in result)
        {
            var inj = new Injury(
                           inj.Id.Value, 
                           inj.Title, 
                           inj.OnDate.Value) ;
            lst.Add(inj);
        }
    }

    return lst;
}

The class Injury is defined in my integration solution an is not part of the  generated file SiteEntities.cs.

But one obvious disadvantage of this tool is the static behaviour. You need to generate the classes each time you change something to be able to reflect the changes in the integration.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s