Write CAML queries in code

February 13, 2008 21:50 by merill

In my very first webpart that I wrote I needed to write some CAML queries to filter and sort the lists.

Being the type of guy who hates seeing strings littering my code I searched the net for an API for CAML queries. Thankfully John Holliday had already done the hard work and created CAML.NET. Check out the clean syntax for the CAML code.

   1: string typeName = "My Content Type";
   2: string simpleQuery =
   3:     CAML.Query(
   4:         CAML.Where(
   5:             CAML.Or(
   6:                 CAML.Eq(
   7:                     CAML.FieldRef("ContentType"), 
   8:                     CAML.Value(typeName)),
   9:                 CAML.IsNotNull(
  10:                     CAML.FieldRef("Description")))),
  11:         CAML.GroupBy(
  12:             true,
  13:             CAML.FieldRef("Title",CAML.SortType.Descending)),
  14:         CAML.OrderBy(
  15:             CAML.FieldRef("_Author"),
  16:             CAML.FieldRef("AuthoringDate"),
  17:             CAML.FieldRef("AssignedTo",CAML.SortType.Ascending))
  18:     );

Here again you might run into the issue of adding an extra assembly to your deployment, the simple alternative that I took was to include the CAML class directly into me source tree. But remember to keep the copyright and license header in place as stated by the Ms-CL license.

Hopefully Microsoft will release a Linq to CAML library soon, there is already one at CodePlex by a Microsoft employee but what would be better is if it comes with SharePoint.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Getting ModifyUserPropertyByAccountName to work

February 5, 2008 13:47 by admin

The documentation for the ModifyUserPropertyByAccountName method of the UserProfileService in the SharePoint Server SDK has a bug.

You'll notice that although there are no errors reported the profile doesn't get updated. The fix is quite simple though and is actually present in the other examples. Setting the IsValueChanged to true does the trick.

Here's how the corrected code looks like.

   1: namespace ModifyUserPropertyByNameSample
   2: {
   3:     class Program
   4:     {
   5:            
   6:         static void Main(string[] args)
   7:         {
   8:             //Instantiate the Web service. 
   9:             UserProfileService userProfileService = 
  10:                 new UserProfileService();
  11:  
  12:             //Set credentials for requests.
  13:             //Use the current user log-on credentials.
  14:             userProfileService.Credentials =
  15:                 System.Net.CredentialCache.DefaultCredentials;
  16:  
  17:             PropertyData[] newdata = new PropertyData[1];
  18:             newdata[0] = new PropertyData();
  19:             
  20:             newdata[0].Name = "FirstName";
  21:             newData[0].IsValueChanged = true;
  22:  
  23:             newdata[0].Values = new ValueData[1];
  24:             newdata[0].Values[0] = new ValueData();
  25:             newdata[0].Values[0].Value = "Mark";
  26:  
  27:             // TODO 
  28:             // Replace "domain\\username" with valid values.
  29:             userProfileService.ModifyUserPropertyByAccountName
  30:                 ("domain\\username", newdata);
  31:  
  32:         }
  33:     }
  34: }

 

For what it's worth I've uploaded a working solution to the MSDN Code Gallery that allows you to view the current profile data and make changes by providing the name/value pair through the query string. You can download the SharePoint Profile Updater here.


Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Efficiently Delete / Purge All Items from a SharePoint List

February 4, 2008 14:01 by admin

If you want to delete or remove all the items in a SharePoint list you need to iterate through each item and perform a delete. Most of the examples on the web iterate through each item to do the delete. The problem there is that each delete action makes a request to the server and a huge list is going to take a long time. You could always delete the entire list and recreate it but that would mean recreating the structure as well as the guid being changed.

The better method would be to make a single request with all the delete commands batched together as shown in the code sample below.

   1: private static StringBuilder BuildBatchDeleteCommand(SPList spList)
   2: {
   3:     StringBuilder sbDelete = new StringBuilder();
   4:     sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
   5:     string command = "<Method><SetList Scope=\"Request\">" + spList.ID + 
   6:         "</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
   7:  
   8:     foreach (SPListItem item in spList.Items)
   9:     {
  10:         sbDelete.Append(string.Format(command, item.ID.ToString()));
  11:     }
  12:     sbDelete.Append("</Batch>");
  13:     return sbDelete;
  14: }

I've published a working solution on the MSDN Code Gallery at http://code.msdn.microsoft.com/SharePointListDelete.


Currently rated 4.0 by 3 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5