5

In my Visual webpart I need to, based on the name of a list get the ID of the list, dynamically. I'm using C#.

How can this be done?

4
  • Can you please add some details on what you are tring to do? Alos you have tagged the question with SPServices, and C#. Those are very different frameworks. Oct 20, 2015 at 7:07
  • i have List name in that that list name i want to getting That List ID
    – SNGOKUL
    Oct 20, 2015 at 8:08
  • please help this post
    – SNGOKUL
    Oct 20, 2015 at 8:08
  • Client object model or server object model? Oct 20, 2015 at 8:13

2 Answers 2

4

You should always select your list based on the URL, as the Title is more likely to change. And don't do SPWeb.Lists as this will enumerate all lists on the web site. Use this similar snippet instead:

public Guid? GetListId(string siteUrl, string listUrl)
{
    using (var currentSite = new SPSite(siteUrl))
    {
        using (var currentWeb = currentSite.OpenWeb())
        {
            try
            {
                var spList = currentWeb.GetList(SPUtility.ConcatUrls(currentWeb.ServerRelativeUrl, listUrl));
                return spList.ID;
            } catch (Exception ex) {
                //List was not found.
                return null;
            }
        }
    }
}
5
  • 1
    How are you returning the GUID? Your function seems to have a return type of GUID, but there is no return statement.
    – Asad Refai
    Oct 20, 2015 at 8:50
  • In a magical way :) Updated the answer. Oct 20, 2015 at 8:52
  • This will not compile, you are not returning anything in case of an exception ;) Oct 20, 2015 at 8:54
  • 1
    The example was to provide an understanding of the concept. OP will change the snippet most probably. Updated anyway :) Oct 20, 2015 at 8:57
  • I know I know, just wanted to be a little bit of a pain in the ass ;) Oct 20, 2015 at 8:59
0

You can try following method if you are using Server Object Model. I did not test my code. Written from top of head. test and let me know

public Guid? GetListId(string siteUrl, string listName)
        {
            using (var currentSite = new SPSite(siteUrl))
            {
                using (var currentWeb = currentSite.OpenWeb())
                {
                    var spList = currentWeb.TryGetList(listName);
                    if (spList != null)
                    {
                        return spList.ID;
                    }else{
                      return null;
                     }
                }
            }
            return null;
        }

Update based on comment

var listIdAsString = GetListId("siteUrl", "list") == null ? "" : GetListId("siteUrl", "list").ToString();
8
  • i used your code but Error in Microsoft.SharePoint.SPListCollection" GetByTitle()" Error Like does not contain definition for "GetByTitle"
    – SNGOKUL
    Oct 20, 2015 at 9:26
  • my answer is update. use TryGetList instead of GetByTitle. Thanks @Robert Oct 20, 2015 at 9:29
  • Ya i use your updated code but Error "GetListId(string siteUrl, string listName)" Error msg: not all code paths return value
    – SNGOKUL
    Oct 20, 2015 at 9:36
  • All paths have return value. Though add else with if. See my update Oct 20, 2015 at 9:43
  • ya i use your code but How to get the return value or how to store the return value into String ?
    – SNGOKUL
    Oct 20, 2015 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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