Load Sharepoint List Items in the drop down list (Web part page) via SP Object Model
private void populateAvailiability()
{
try
{
ddlAvailability.Items.Clear();
string
listName = "HR_Comp_Availability";
SPSite
site = SPContext.Current.Site;
SPWeb
web = SPContext.Current.Web;
Guid
webGuid = web.ID;
Guid
siteGuid = site.ID;
//new
code for spsite
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite currentSite = new SPSite(siteGuid))
{
currentSite.AllowUnsafeUpdates = true;
currentSite.CatchAccessDeniedException = false;
using (SPWeb itemWeb = currentSite.OpenWeb(webGuid))
{
SPList list =
itemWeb.Lists[listName];
SPView view = list.DefaultView;
SPQuery query = new SPQuery();
query.ViewFields = "";
//query.Query =
" ";
SPListItemCollection items = list.GetItems(query);
DataTable Availability2 = items.GetDataTable();
if (Availability2.Rows.Count
> 0)
{
ddlAvailability.DataValueField = "Title";
ddlAvailability.DataTextField = "Title";
ddlAvailability.DataSource = Availability2;
ddlAvailability.DataBind();
//ddlAvailability.Items.Insert(0, new ListItem("select",
"0"));
}
else
{
ddlAvailability.Items.Clear();
}
}
}
});
}
catch (Exception ex)
{
this.lblError.Text += ex.Message + " -- " + ex.StackTrace;
}
}
No comments:
Post a Comment