Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Passing listitemcollection to Generic list

$
0
0

Hi,

I am trying to pass the listitemcollection to a generic list. But I am getting error as Object reference not set.

I have used the below code:

using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
{
 using (SPWeb oWeb = oSite.RootWeb)
{
SPList lstTestLibrary = oWeb.Lists["TESTLibrary"];
SPQuery sQuery = new SPQuery();

sQuery.Query = "<Where><IsNotNull><FieldRef Name='ID' /></IsNotNull></Where>";

 sQuery.ViewFields = string.Concat(
 "<FieldRef Name='Title' />","<FieldRef Name='ID' />","<FieldRef Name='FileLeafRef' />","<FieldRef Name='FileDirRef' />");

sQuery.ViewFieldsOnly = true;

//This commented line works
 // SPListItemCollection myColl = lstTestLibrary.GetItems(sQuery);
     
//Getting error in the below line:                          
List<SPListItem> listItems = lstTestLibrary.GetItems(sQuery).Cast<SPListItem>().ToList();

                           
                           }
                       }

Is it possible to set the data to a Generic list? How to fix this?

Thanks


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>