0

I have a caml query in my code to return some items of SharePoint list. I added the IF statement in case if query not find any matching item or returns NULL.

                   SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery()
                   {
                       Query = @"<Where>
                                   <Eq>
                                     <FieldRef Name='Department'/>
                                     <Value Type='Text'>"+Department+"</Value>
                                   </Eq>
                                 </Where>"
                    });

                    foreach (SPListItem item in Items)
                    {
                        if (item != null)
                        {
                            item["Name"]="abcd";
                            item.Update();
                        }
                        else
                        {
                            newListItem["Name"] = "xyz";
                            newListItem.Update();
                        }
                    }

If it not finds Department in the list, it doesn't goes to ELSE statement.

halfer
  • 19,824
  • 17
  • 99
  • 186
Rizwan
  • 63
  • 2
  • 5
  • 10
  • The for loop will fail i think if your splistitemcollection = NULL. Put a try and catch around it i think your maybe getting an error – Truezplaya Oct 11 '12 at 14:38

3 Answers3

0
         try{
    SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery() 
                        { 
                            Query = @"<Where> 
                                             <Eq> 
                                                <FieldRef Name='Department'/> 
                                                <Value Type='Text'>"+Department+"</Value>                           </Eq></Where>" 
                        }); 
                         if(Items != null){
                        foreach (SPListItem item in Items) 
                        { 
                            if (item != null) 
                            { 
                                item["Name"]="abcd"; 
                                item.Update(); 
                            } 
                            else 
                            { 
                                newListItem["Name"] = "xyz"; 
                                newListItem.Update(); 
                            } 
                        } 

                    }

        }
        Catch(Exception exc){
        //Do something with your exception here
        }
Truezplaya
  • 1,315
  • 1
  • 13
  • 26
  • What are you trying to do? To me i'm not to sure why you you check if item is null in the for loop as item i believe should always besomething if it is in an splistitemcollection. Maybe what you mean to do is check if the title is null. if (item != null) possibly should be if(string.isNullOrEmpty(item[Title].ToString()) – Truezplaya Oct 12 '12 at 07:39
  • if(string.isNullOrEmpty(item[SPBuiltInField.Title].ToString()) – Truezplaya Oct 12 '12 at 07:40
0

i solved it. The query will return only the department that you input. So foreach statement will contain the item in that department only. So i was not finding other items that not matching. it was like:

                       SPListItemCollection Items = RiskAssesment.GetItems(new SPQuery()
                        {
                            Query = @"<Where>
                                         <Eq>
                                            <FieldRef Name='Department'/>
                                            <Value Type='Text'>" + Department + "</Value></Eq></Where>"
                        });

                        if (Items.Count==0)
                        {
                           item["Name"]="abcd"; 
                            item.Update(); 
                        }
                        else
                        {
                            foreach (SPListItem item in Items)
                            {
                                if (item != null)
                                {
                                   item["Name"]="abcd"; 
                            item.Update(); 
                                }
                            }
                        }
Rizwan
  • 63
  • 2
  • 5
  • 10
0

Use item count to know whether there is any item with your choice first. If you have count then use your for-each to loop.

halfer
  • 19,824
  • 17
  • 99
  • 186
Ayyappan Anbalagan
  • 11,022
  • 17
  • 64
  • 94