Ok, so adding to my previous question here - I want to get certain columns from a sharepoint list, then compare values of those columns to items in my own c# list and, if they're different, modify them.

This is what i got so far:

        var web = SPContext.Current.Site.RootWeb;
        SPList list = web.Lists["My Movie List"];

        SPQuery query = new SPQuery();
        query.ViewFields = "<FieldRef Name='Movie WebID' />" +
                           "<FieldRef Name='url' />" +
                           "<FieldRef Name='rating' />";
        query.Folder = list.ParentWeb.GetFolder("My Folder");

        SPListItemCollection listItemCollection = list.GetItems(query);

        foreach(SPListItem item in listItemCollection)
            int itemID = movies.FindIndex(movie => movie.ID == item["Movie WebID"]);
            if ( item["url"] != movies[itemID].URL1 || item["url"] != movies[itemID].RATING1)
                item["url"] = movies[itemID].URL1;
                item["rating"] = movies[itemID].RATING1;

Could somebody check if the syntax is correct and if there is a better way to write that code?

Also I'm confused about the item["rating"] field, as rating is an integer - is there some casting necessary then?

2 Answers 2


The code looks fine, but you might want to put in some error handling as you will get Exceptions if you try to use a field value (listItem["someField"]) and either that field does not exist, or is null and you try to use the value.

Also, I think that line:

 if ( item["url"] != movies[itemID].URL1 || item["url"] != movies[itemID].RATING1)

Should be

 if ( item["url"] != movies[itemID].URL1 || item["rating"] != movies[itemID].RATING1)


Lastly instead of using

SPList list = web.Lists["My Movie List"];

You should get the list using its URL, which is more efficient, more portable in different languages and more importantly will not totaly break if someone renames the list.


When getting columns using item["ColumnName"] you're not allowed to use space like you do with item["Movie WebID"]. You should use internalname of the column.

  1. Go to List Settings
  2. Scroll down on page to Columns section
  3. In the list of Columns, click on the column names
  4. View the address bar of the browser and look for the querystring part: ?List=%7B4F5EF009%2D3318%2D4BEC%2D9EB4%2DC08DE6CEC483%7D&Field=MyColumn
  5. Then look for the '&Field='
  6. Mark the value after '=' and use that in your code.
  • fixed it, but the code won't let me even go further than the first line: int itemID = movies.FindIndex(movie => movie.ID == item["Movie_x0020_WebID"]); Web Part Error: Unhandled exception was thrown by the sandboxed code wrapper's Execute method in the partial trust app domain: An unexpected error has occurred. Commented Jul 24, 2012 at 11:01
  • Same for "FieldRef Name='Movie WebID'", that doesn't look like an internal field name.
    – Louis
    Commented Jul 24, 2012 at 11:02
  • You get that exception because of the incorrect FieldRef Name. Your CAML Query is not fetching the field 'Movie_x0020_WebID', and therefore when you try to access it in the item it does not exist.
    – Louis
    Commented Jul 24, 2012 at 11:09
  • Right, I'm an idiot, thank you. OK, fixed that too, and now it gives me that error on the first line inside the if statement. Also itemID is always -1, so there's still something wrong. Commented Jul 24, 2012 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.