Skip to content


Access SharePoint API from Console Program – Program Against the WSS Object Model

Sometimes it is needed to make small changes to a lot of listitems etc. in SharePoint, and opening them all up in the API and editing them is not an options. Little console programs are often convenient. It’s actually also quite easy to do something like that.

In this small Console example we will change the SharePoint rootwebs title programmatically:

1: Open up Visual Studio and create a new Console program.

2: Rightclick “References” in “Solution Explorer” and choose “Add Reference”. In the .NET tab scroll to the bottom, and choose the one called “Windows SharePoint Services”.

That’s the normal WSS API DLL. WSS is where you do most of your developing in SharePoint since WSS is the core of it all. MOSS just adds extra functionality on top of WSS. Actually it just adds some “features” (yes normal ones) on top of WSS.

3: Add “Using Microsoft.SharePoint;”

4: Open a SharePoint site and web and edit what you wish. Edited lists, webs, sites etc, must be “updated” using web.Update(), myList.Update() etc. in order to actually make the changes.

01
using System;
02
using System.Collections.Generic;
03
using System.Linq;
04
using System.Text;
05
using Microsoft.SharePoint;
06
 
07
namespace SharePointAPI
08
{
09
  class Program
10
  {
11
    static void Main(string[] args)
12
    {
13
      SPSite site = new SPSite("http://tsd-mosstest");
14
      SPWeb web = site.OpenWeb(); //Opens rootweb
15
 
16
      web.Title = "Edited Title";
17
 
18
      web.Update(); //remember this to actually "save".
19
 
20
      //When and how to dispose is a heated discuttion in SharePoint
21
      //and not a discussion I'm gonna go into now. 
22
      //You can also use "using".
23
      web.Dispose();
24
      site.Dispose();
25
    }
26
  }
27
}
28

Execute the console program and you will see that the websites title has changed now. You can offcourse do a lot more! You can access lists and insert items in them, add lists etc. You can pretty much do everything you can do through the GUI and then some.
This is not really how you should add lists and such. That should always be done with Features.

Note: Everything you do through code in features, featurerecievers, listevents etc etc, can be done in console programs as well. Most code you write in your application pages and codebehind can be done through the API. You can even create pages instead of clicking through the GUI.

DeliciousDiggTwitterFacebookLinkedInTechNetStumbleUponNewsVineTechnorati FavoritesSlashdotMSDNShare

Related posts:

  1. Object reference not set to an instance of an object. c:\wss\EventPlanning.wsp: The Solution installation failed.
  2. Object reference not set to an instance of an object, when running STSADM commands
  3. Error when updating Windows SharePoint Services 3.0 with SP2 – An update conflict has occurred, and you must re-try this action. The object SPSearchDatabase…
  4. stsadm is not recognized as an internal or external command, operable program or batch file.
  5. How to create a new feature in sharepoint using Visual Studio

Posted in SharePoint 2007.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.



Page optimized by WP Minify WordPress Plugin