Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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;
                item.Update();
            }
        }

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?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
add comment

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.
share|improve this answer
    
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. –  Rafał Saltarski Jul 24 '12 at 11:01
    
Same for "FieldRef Name='Movie WebID'", that doesn't look like an internal field name. –  Louis Jul 24 '12 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 Jul 24 '12 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. –  Rafał Saltarski Jul 24 '12 at 11:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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