Get Items from a SharePoint List

Hello,

i starting with programming Webparts for SharePoint in C# with Visual Studio. I want to get Items from a SharePoint List which are depend on a specific input from a user.

My List have a Name, E-Mail-Adress and Informations.

So i startet to create a TextBox (get_UserName) where a user kann Enter die Name for which person he want to get the information. When he click on the Button, i want to get informationen about the person in a label from the sharepoint list. 

But i don't know how to get these information from the list. I like to have a statement like this for SharePoint

"Select * from List where name " + get_UserName

Can anybody help me to get information from a list and write them down in a label when a user click on the button?

Another problem is, that i want to get a status during my coding. So i write down System.diagnostics.Debug("Test"); but i cannot see anything on the visual basic output window. Anybody have a solution for this problem? I tested the debug modus and normal but i never see anything when i deploy the webpart....

July 20th, 2012 9:38am

About requesting SharePoint List.

You should start here.

You will see that CAML is used to query list. It is like SQL for SharePoint.

I will then advice you to use a tool called Caml Builder. You can download the SharePoint 2007 version.

This tool will allow you to create your query in the designer, you can then copy past it in your code.

About your other issue, it is odd because I use System.Diagnostics.Debug.Writeline("Test") and it works great.

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 9:44am

Hello,

you can get data from your list with CAML query

somethin like this

SPList list = web.GetList(SPUrlUtility.CombineUrl(web.Url, listUrl)); 
 SPQuery spquery = new SPQuery();
 spquery.Query = string.Format(@"<Where><Eq><FieldRef Name='UserName' /><Value Type='Text'>{0}</Value></Eq></Where>", textboxUSername.Text);
  SPListItemCollection collection = list.GetItems(spquery);
  if (collection.Count > 0)
 {
 //do your stuff with collection[0]["FieldToDisplay"]
}
      
just change the field name in your query

I've made a topic about getting information from user input here : http://christopherclementen.wordpress.com/2012/04/02/filter-a-list-dynamically/

Hope this helps !

July 20th, 2012 9:46am

Thank you. I will read the page about CAML.

Sorry, i also user System.Diagnostics.Debug.Writeline("Test"), but i didn't get any output. Can you take a screenshot where the output is in visual studio 2010?

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 9:47am


About your other issue, it is odd because I use System.Diagnostics.Debug.Writeline("Test") and it works great.

July 20th, 2012 9:49am

Hi

You can query a list using CAML, so if your required name is input via a text box, you can run the following code snippet.

string name = txtBox.Text;

SPQuery query = new SPQuery(); query.Viewfields ="<FieldRef Name='Name'/><FieldRef Name='Email'/></FieldRef Name='Information' />"; query.Query ="<Where><FieldRef Name='Name'/><Value Type='Text'>"+name+"</Value></Where>"; SPList list = SPContext.Current.Web.Lists.TryGetList("your_list_name"); SPListItemCollections items = list.GetItems(query); foreach (SPListItem item in items) { string name = item["Name"]; string email = item["Email"]; string info = item["Information"]; }

I have done it simple here for demonstration, so I have added a foreach loop that shoould loop only once if your query parameter is unique for the list. Remember to set the FieldRef Name values and the item["index"] values according to your list column names. Then it should be fairly easy to add the results to a Label for display.

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 9:53am

here you go : screenshot

Needless to say that you need to be in debug

July 20th, 2012 9:53am

Thats the same, but i didn't get anything on the output of the debug mode....

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 10:45am

I get the information on the output. It issnt Output in the Debugmodus it's called "Direktausgabe" in my VisualStudio.

I have a problem this:

string name = item["Name"];

Error code: object cannot convert to string


July 20th, 2012 11:13am

Yes I guess you have Visual Studio installed in german or something :D

You should have a reference to Microsoft.SharePoint.dll

but if you have used the SharePoint Project Template (which is by default in VS), it should be there already.

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 11:17am

Yes, i install it in german, but i have two output windows "Ausgabe" and "Direktausgabe" and the correct translation for Output is "Ausgabe" and not "Direktausgabe" ;-)

I create a SharePoint Project and add a new element Webpart. I didn't have the reference Microsoft.SharePoint.dll

Can i download it? :D

July 20th, 2012 11:28am

It's in your SharePoint 14 hive, on the machine where SharePoint 2010 is installed

C:\program files\common files\microsoft shared\web server extensions\14\ISAPI

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 11:33am

You can use

string name = item["Name"].ToString();

July 20th, 2012 11:35am

I have the feeling that you are not developing on a machine where SharePoint installed.

I would definitely recommend.

You can install SharePoint 2010 on Windows 7 or Windows Server

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 11:49am

Hmmm... i go to References and add a new reference, but after click on OK i cannot see Microsoft.SharePoint.dll under references and cannot use ist in my code...

July 20th, 2012 11:51am

Click on the "Browse" button in the Add Reference Dialog and locate it in the path I gave you above..
Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 12:04pm

That may be the case here,  but you will normally get an error message if you try to create a SharePoint project if SharePoint is not installed on the machine you are developing on. The same message can be removed by a Registry setting btw.
July 20th, 2012 12:09pm

"Click on the "Browse" button in the Add Reference Dialog and locate it in the path I gave you above.." -> thats the way i do it

I had installed SharePoint 2010 on the machine and Visual Studio 2010...

Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 12:29pm

Can you check your target framework in your solution properties?

ensure that you are not under 4.0 , you should put 3.5

July 20th, 2012 12:32pm

The Framework for the Project is 3.5
Free Windows Admin Tool Kit Click here and download it now
July 20th, 2012 12:41pm

So you have the ISAPI folder but the .dll is not there ?

July 20th, 2012 1:34pm

No. I have the folder and can add it to the references in my visual studio project, but when i click on references in the project i can't see the .dll and i cannot use it in my project....
Free Windows Admin Tool Kit Click here and download it now
July 23rd, 2012 7:37am

You can also get items and perform multiple operations on it using Call Http service action in wworkflow manager.

https://zareensoomro.wordpress.com/2015/07/02/get-items-from-sharepoint-list-using-call-http-web-service-action-sharepoint-designer-2013/

July 3rd, 2015 1:01am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics