Faster way to manipulate SPListItems

by Martin 19. december 2008 12:20

Sometimes in a Sharepoint installation, it's required to manipulate a lot of ListItems based on some specific criterias. This is normally done by iteraterating the whole site and subsites (and the subsites' subsites, etc). Doing this trough the object model and API is pretty straight forward to most developers, but very tedious to do when considering the time it takes to iterate a large site. There is a way to do it faster, which i'm going to demonstrate below. The basic idea is to "plug directly in" to the Content Database of the site in question, and get the needed ID's to make up a SPListItem-object.

The following example shows how to get all ListItems (which has an .aspx-extension) based on a certain contenttype (you just insert the name of the content type). After getting the data from the database, it's pretty straight forward to make a SPListItem-object from the ID's as shown. Basically we just READ from the database and make changes through the API. I'm not quite sure, if this is legal in terms of support from MS. But I don't think you break the warranty, because you only READ from the Content Database. Remember: NEVER manipulate the Content Database directly, this will leave your installation in an unsupported state. Shouts go out to my co-worker and colleague Ulrich who gave me the bits, I just changed them to fit my needs!

I haven't done the math on this one, but it's certainly way faster than iterating through the whole site. A query like this takes at the most (for the large installation I'm working with (around 300+ sites in a site colleciton)) 5 seconds.

And remember kids: always dispose your webs and sites either by using .Dispose() or by using... using() Smile (If i've forgot to do it in the code below, please slap me silly)

using (SPSite site = new SPSite("http://test/"))
   {
    SPContentDatabase db = site.ContentDatabase;

    SqlCommand c = new SqlCommand();
    string strConn = "Integrated Security=SSPI;Server=" + db.Server + ";database=" + db.Name;

    using (SqlConnection conn = new SqlConnection(strConn))
    {
     c.Connection = conn;
     try
     {
      conn.Open();

      string commandText = "select distinct(A.Id), A.leafname, A.listid, A.WebId as WebId from  alldocs A, alluserdata B where A.extension='aspx' and A.leafname = B.tp_leafname and A.listid = B.tp_listid and B.tp_Contenttype = '" + ContentTypeNameGoesHereAsString + "' order by A.webid";

      SqlDataAdapter adt = new SqlDataAdapter(commandText, conn);

      DataSet ds = new DataSet();

      adt.Fill(ds);

      if (ds.Tables.Count != 0)
      {
       ds.Tables[0].DefaultView.Sort = "WebId asc";
       SPWeb web = null;

       #region Iterate data
       foreach (DataRow dr in ds.Tables[0].Rows)
       {
        Guid id = (Guid)(dr[0]);
        string leafName = dr[1] as string;
        Guid listId = (Guid)(dr[2]);
        Guid webId = (Guid)(dr[3]);

        if (web == null)
        {
         web = site.OpenWeb(webId);
         Console.WriteLine("Now processing: " + web.Url);
        }
        else if (webId != web.ID)
        {
         //Clean up nice
         web.Dispose();
         //Create new web-instance
         web = site.OpenWeb(webId);
         Console.WriteLine("Now processing: " + web.Url);
        }
        SPListItem itm = web.Lists[listId].Items[id];

       [THIS IS WHERE YOUR CODE MANUPULATING THE LISTITEM GOES!]
       }
       #endregion

       if (web != null)
       {
        web.Dispose();
       }
      }
     }
     catch (Exception Ex)
     {
     }
    }
   

DISCLAIMER: Use the above at own risk! The author can not be held responsible for any damage to your Sharepoint installation you may encounter using the above example!

Tags: ,

.NET

Tilføj kommentar

  Country flag

biuquote
  • Kommentar
  • Eksempel
Loading

Page List

About

Take a look at my LinkedIn-profile.

DISCALIMER: The content on this blog is in NO way affiliated with my employer. The opinions expressed on this blog are solely my own and in NO way endorsed by my employer.

Recent comments

Comment RSS

Month List