1

I Am trying to bind a custom list from http://mysharepoint/subsite/subsite/lists/sourcelist with no luck. Any thoughts why it's not working?

 using (SPSite site = new SPSite(SPContext.Current.Site.Url))
     using (SPWeb web = site.OpenWeb())
         {
            SPList listCategory = web.Lists["http://mysharepoint/subsite/subsite/Lists/sourcelist"];
            ddlCategory.DataSource = listCategory.Items;
            ddlCategory.DataValueField = "ID";
            ddlCategory.DataTextField = "Title";
            ddlCategory.DataBind();
         }
  • You can not get the list by its URL that way I think? "Use an indexer to return a single list from the collection. For example, assuming the collection is assigned to a variable named collLists, use collLists[index] in C#, or collLists(index) in Visual Basic, where index is the index number of the list in the collection, the display name of the list, or the GUID of the list.", msdn.microsoft.com/en-us/library/… – Robert Lindgren Jan 14 '16 at 14:46
  • Quite confusing, I have the same line of code binded to another dropdownlist and work just fine. In comparison with the above, the list was located in sub sites. Both codes are identical except the web.Lists path. – Jonathan.B Jan 14 '16 at 16:52
  • 1
    Jonathan.B...is this code being executed within the subsite? If not, then SPContext.Current.Site.Url is going to be referencing the root site and won't find the list. Robert is correct, you only need the name or GUID for the list. – jpollar Jan 14 '16 at 18:34
0

Manage to make it work. Thanks for the reply.

using (SPSite site = new SPSite(SPContext.Current.Site.Url))
using (SPWeb web = site.OpenWeb("/subsite/subsite")) //site source
     {
        SPList listCategory = web.Lists["sourcelist"]; //the list name
        ddlCategory.DataSource = listCategory.Items;
        ddlCategory.DataValueField = "ID";
        ddlCategory.DataTextField = "Title";
        ddlCategory.DataBind();
     }
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.