RSS

Programatically select and bind Sharepoint list items to ASP.NET controls

14 Nov

  • Open Visual Studio 2010
  • Create New Project
  • Select your Sharepoint site
  • Create Visual Webpart
  • Add LAbels,Textboxes or Dropdownlist (NOTE: Update control names in your code accordingly)
  • Use below code in page load event

try
{

SPQuery query = new SPQuery();
query.Query = “”;
query.ViewFields = “”;
query.RowLimit = 100;

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{

using (SPWeb web = site.OpenWeb())
{

SPList list = web.Lists.TryGetList(“—ADD LIST NAME—“);

//Check List Exists

if (list != null)

{

//Check ITEMS Exists

if (list.GetItems(query).GetDataTable() != null)
{
DataTableReader rdr = list.GetItems(query).GetDataTable().CreateDataReader();

if (rdr.Read())

//Bind list items to ASP.NET Label

Label1.Text = rdr[“Test_x0020_Item1”].ToString();
Label2.Text = rdr[“TestField2”].ToString();

//To bind DROPDOWNLIST uncomment below code
// dropdownlist1.DataSource = rdr;
// dropdownlist1.DataTextField = “Title”;
// dropdownlist1.DataValueField = “Title”;
// dropdownlist1.DataBind();

//To bind REPEATER uncomment below code
// Repeater1.DataSource = rdr;
// Repeater1.DataBind();

rdr.Close();

}
else
{
lblInfo.Text = “No Items Found”;
}
}
else
{
lblErrorMessage.Text = “List not found.Please make sure list is created and go to webpart settings to add the List name “;
}
}

}
}

catch (Exception ex)
{
lblErrorMessage.Text = ex.Message.ToString();
}

Advertisements
 
 

Tags: ,

6 responses to “Programatically select and bind Sharepoint list items to ASP.NET controls

  1. venu

    November 24, 2012 at 12:08 AM

    nice blog.I have a requirement for creating a login page with 2 text boxes and 2 button controls with 3 users master,admin and user,depending on the credentials given they should go to the respective pages.my requirement is to save details in the list and from that list (username and password), bind it to the textboxes .Can you please help me with requirement.

    thanks
    venu

     
    • Jim Mathew

      November 24, 2012 at 11:34 AM

      Thanks Venu! for your comments. I would recommend to go for Forms Based Authentication (FBA) and use SQL database for managing users. better to use default ASP.NET membership wizad to auto generate the tables. Please refer below links.


      or you can use the readymade webparts from codeplex

       
  2. venu

    December 1, 2012 at 11:19 PM

    Thanks Jim! for your reply.Can you please suggest to create the webpart,as i cant find the links.Can you please help me with the code or any links.

    Thanks
    venu

     
  3. Hassan

    May 13, 2014 at 5:37 PM

    Great post, but this selects all fields from sql table of list, what will be query if I want to select some specific fields and not the sharepoint list internal fields

     
    • Hassan

      May 18, 2014 at 2:57 PM

      Got solution set query.viewitemsonly equal to true 😉

       

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: