List match list

In code, how can I access a list, such as โ€œMyListโ€ in sharepoint, then iterate over these list items and get the value of a specific column in that list, such as the โ€œURLโ€ column?

+6
c # sharepoint
source share
8 answers

From this blog post :

The proper way to do this is to store the return value of the Items property in the SPListItemCollection variable. In this case, the database is requested only once, and then we will iterate over the result set, which is stored inside the collection object. Here is the modified sample code:

SPListItemCollection items = SPContext.Current.List.Items; for(int i=0;i<100 && i<items.Count;i++) { SPListItem listItem = items[i]; htmlWriter.Write(listItem["Title"]); } 
+2
source share

To get all the elements from the list and iterate over each of them, the best solution would be the following (assuming that this code runs as part of the function):

 public override void FeatureActivated(SPFeatureReceiverProperties properties) { using(SPSite site = properties.Feature.Parent as SPSite) { SPList list = site.RootWeb.Lists["ListName"]; SPListItemCollection items = list.Items; foreach (SPListItem listItem in items) { Response.Write(SPEncode.HtmlEncode(listItem["Url"].ToString()) +"<BR>"); } } } 

But if the list is very large, it would be better to break the pages into list items:

 public override void FeatureActivated(SPFeatureReceiverProperties properties) { using(SPSite site = properties.Feature.Parent as SPSite) { SPList list = site.RootWeb.Lists["ListName"]; if(items.ItemCount > 100) { SPQuery query = new SPQuery(); query.RowLimit = 100; int index = 1; do { SPListItemCollection items = list.GetItems(query); foreach (SPListItem listItem in items) { Response.Write(SPEncode.HtmlEncode(listItem["Url"].ToString()) +"<BR>"); } query.ListItemCollectionPosition = items.ListItemCollectionPosition; index++; } while (query.ListItemCollectionPosition != null); } else { SPListItemCollection items = list.Items; foreach (SPListItem listItem in items) { Response.Write(SPEncode.HtmlEncode(listItem["Url"].ToString()) +"<BR>"); } } } } 

It is based on Microsoft 's Best Practices for SharePoint.

+2
source share

You can also iterate over elements directly, and if you use the URL field, you probably want to use the SPFieldUrlValue class, so you don't have to deal with how SharePoint stores URLs:

 foreach(SPListItem item in spList.Items){ SPFieldUrlValue data = item["Url"] as SPFieldUrlValue; // now you have data.Description, data.Url } 

There are many such SPField* helper classes, and they are very useful, especially if you have multiple values.


Edit:

For some reason, some people believe that this path is slower, based on the evidence in the blog post on Greg (even voted). This, however, has nothing to do with my answer: the foreach creates an Iterator, so it should not access the database 99 more times (in the message they used the for loop to access the first 100 elements),

+1
source share

if you are in a function, the function is activated in a specific area (for example, a site, website, web application or farm).

When you want to access the list from this function, use the SPFeatureReceiver class to bind the event receiver to your function. Then there are overrides in this class when an event is triggered by a function. that the override gets a parameter of type SPFeatureReceiverProperties.

from this parameter you can use get on the site:

 public override void FeatureActivated(SPFeatureReceiverProperties properties) { using(SPSite site = properties.Feature.Parent as SPSite) //this depends on scope of feature { SPList myList = site.RootWeb.Lists["MyList"]; } } 

for how to iterate this list see orther answers

0
source share

As others have said, you should not iterate over the Items collection directly (especially in large collections). There is alternaltive here:

// if you need the whole collection. Otherwise use SPQuery in the list

 DataTable dt = list.Items.GetDataTable(); foreach (DataRow row in dt.Rows) { ... 

Then you can do many things. If you need to check to get only some of the elements, for example:

  if (row["ContentType"].ToString().Equals("Your contenttype id")) { SPListItem item = list.GetItemById((int)row["ID"]); 

or use SpQuery to get the column in the query, for example:

 SPQuery oQuery = new SPQuery(); oQuery.ViewFields = "<FieldRef Name='UrlColumn'/>"; list.Items.GetItems(oQuery).GetDataTable(); ...foreach code... row["UrlColumn"] 
-one
source share

If you're in an x86 environment, I recently discovered a surprisingly readable way to retrieve data using MSSQL / OLEDB ...

 SELECT * FROM OPENROWSET ( 'Microsoft.ACE.OLEDB.12.0', 'WSS;IMEX=1;RetrieveIds=Yes;DATABASE=http://sharepoint.lsi.local/ops/;LIST={3DCAF100-44A1-4331-8328-748AA98E36AB};', 'SELECT * FROM list' ) 

http://www.connectionstrings.com/sharepoint

-one
source share

BTW When using OPENROWSET ...

IMEX = 2 for read / write. IMEX = 1 is ReadOnly.

List = [Name] works for me instead of using list = {GUID}.

-one
source share

I know this question was asked a very long time, but I hope I can help someone now :)

This is how I was able to do this

 protected void Page_Load(object sender, EventArgs e) { // Get the current domain var current = HttpContext.Current.Request.Url.Host; // We need to tell the SPSite object the URL of where our List is stored. // Make sure you have the right location placed after the 'current' variable using (SPSite spSite = new SPSite("http://"+current+"/DirectoryForLists/Lists")) { // Returns the Web site that is located at the specified server-relative or site-relative URL. using (SPWeb spWeb = spSite.OpenWeb()) { //Get our list we created. SPList list = spWeb.Lists["Name Of the List"]; // Create a new SPQuery object that will hold our CAML query. SPQuery q = new SPQuery(); // CAML query. // This allows you to controll how you receieve your data. // Make the data ASC or DESC. Find more on MSDN. q.Query = "<OrderBy><FieldRef Name='DESCR' Ascending='TRUE' /></OrderBy>"; // We put our list data into a SP list Item Collection. // Notice that the CAML query is the only parameter for the GetItems() function. SPListItemCollection items = list.GetItems(q); // Here you can loop through your list. foreach (SPListItem spItem in items) { // Your code here. MessageBox(spItem); // Get URL column MessageBox(spItem["URL"]); // Another Column MessageBox(spItem["DateTime"]); } } } } 
-one
source share

All Articles