Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
How to get item value under one column in a list using sharepoint object model

Answered How to get item value under one column in a list using sharepoint object model

  • Thursday, January 26, 2012 2:50 PM
     
     
    I have a list called "TEST" under TEST I have two columns as TEST1,TEST2. I have only one Item in that list. I need to get Value of Item under the TEST1. How can I get the value? Please help me in this regard

All Replies

  • Thursday, January 26, 2012 3:15 PM
     
      Has Code

    Where do you want to fetch this value? Inside a webpart or in some event?

    SPWeb web = SPContext.Current.Web;
    
    SPList list = web.Lists.TryGetList("TEST");
    
    SPListItemCollection items = list.Items;
    string val = String.Empty;
    foreach(SPListItem item in items)
    {
       val = item["TEST1"].ToString();
    }
    

    This is a sample code as in your case list contains only one item only.

    In-case you are using an event receiver then you need to modify the code as SPContext is null there.

    Please let me know if you need help in implementing this

  • Thursday, January 26, 2012 3:30 PM
     
     

    Thanks for the reply Geetanjali, I have written code as below I got output as: 7;#Rajashekar. why I am getting 7;#

     

     using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                {
                    SPWeb web = site.OpenWeb();

                    SPList list = web.Lists.TryGetList("Departments");

                    SPListItemCollection items = list.Items;
                    string val = String.Empty;
                    foreach (SPListItem item in items)
                    {
                        val = item["HRName"].ToString();
                        Response.Write(val);
                    }

    }

  • Thursday, January 26, 2012 3:41 PM
     
     Answered Has Code

    Since your field is of type User , it is coming like this.

    Try

    SPFieldUser userField = (SPFieldUser)item.Fields.GetField("HRName");
                            SPFieldUserValue userValue = (SPFieldUserValue)userField.GetFieldValue(item[HRName].ToString());
                            SPUser user = userValue.User;
                            val = user.LoginName
    

     

  • Thursday, January 26, 2012 4:22 PM
     
     
    How can we do if it the list having multiple items?
  • Thursday, January 26, 2012 4:27 PM
     
      Has Code
    Collection<string> userNames = new Collection<string>();
    
    foreach (SPListItem item in items)
                    {
                       SPFieldUser userField = (SPFieldUser)item.Fields.GetField("HRName");
                            SPFieldUserValue userValue = (SPFieldUserValue)userField.GetFieldValue(item[HRName].ToString());
                            SPUser user = userValue.User;
                            val = user.LoginName
    
    userNames.Add(val);
                    }
    
    foreach(string username in userNames)
    {
       //The way you want to use it
    }
    
    


    foreach loop will help you find it for each and every item. You can create a collection of usernames or whatever may be your requirement and then use it accordingly

     

  • Thursday, January 26, 2012 6:24 PM
     
     

    I want to assign above "val"  in my below workflow code.

                taskproperties.AssignedTo = @"rwtest\raja"; instead of  @"rwtest\raja" this I want "val"

     

    I have included the above lines in the below code but it is giving error how to include the above code in the below one work.

    private void createTask1_MethodInvoking(object sender, EventArgs e)
            {
              
                SPListItem item = workflowProperties.Item;
                TaskID = Guid.NewGuid();
                taskproperties.Title = "Task for " + workflowProperties.Item.Name;
                taskproperties.AssignedTo = @"rwtest\raja";
                taskproperties.DueDate = DateTime.Now.AddDays(1.0);
                taskproperties.ExtendedProperties["Comment"] = "Please review the leave...";
                createTask1.TaskProperties = taskproperties;
            }

  • Thursday, January 26, 2012 6:43 PM
     
     

    What error do you get when you try to do

     taskproperties.AssignedTo = val;

     

  • Thursday, January 26, 2012 6:54 PM
     
     

    I got the exception as [System.NullReferenceException]    {"Object reference not set to an instance of an object."}

    at SPSite site. below is code

    Can u please tell how should I write the proper code

     

     private void createTask1_MethodInvoking(object sender, EventArgs e)
            {


                SPListItem item = workflowProperties.Item;
                TaskID = Guid.NewGuid();
                taskproperties.Title = "Task for " + workflowProperties.Item.Name;
               // taskproperties.AssignedTo = @"rwtest\sida";
                taskproperties.DueDate = DateTime.Now.AddDays(1.0);
                taskproperties.ExtendedProperties["Comment"] = "Please review the leave...";
                createTask1.TaskProperties = taskproperties;
                try
                {

                    using (SPSite site = new SPSite(SPContext.Current.Web.Url))
                    {
                        SPWeb web = site.OpenWeb();

                        SPList list = web.Lists["Departments"];
                        SPListItemCollection items = list.Items;
                        string val2 = String.Empty;
                        foreach (SPListItem itemv in items)
                        {
                            string creator = itemv["HRName"].ToString();

                            SPUser hr = web.SiteUsers.GetByID(Convert.ToInt32(creator.Substring(0, creator.IndexOf(';'))));

                            val2 = hr.LoginName;

                            taskproperties.AssignedTo = val2;
                        }
                    }
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }

  • Thursday, January 26, 2012 7:02 PM
     
     Answered

    The problem is that the workflow runs in a different context so your SPContext is null.

    You cannot obtain the url by doing SPContext.Current.Web.Url

    Either if you know the url simply enter the url or you will have to pick that from property bag.

  • Thursday, January 26, 2012 7:11 PM
     
     
    You are great!!!! I hard coded the URL then its start working THANKYOU VERY MUCH...THANKS FOR YOUR HELP
  • Thursday, January 26, 2012 7:13 PM
     
     
    Glad to hear that it solved your issue. :)
  • Friday, January 27, 2012 2:44 AM
     
     

    Hi Geethanjali Good Morning! Have A GREAT DAY!

    In the abov.e code I mentioned I can get HR Name , Like same I have "DH Name","MGR" columns also there in Departments List

    I need to assign DH name in some other method called MethodInvoking1.

    I need to assign MGR name in some other method called MethodInvoking2

    In these methods also I need I need to write the code like spsite object retive and get the DH name.

    Can we have one global method and I have to give input as column name like that..

  • Friday, January 27, 2012 3:45 AM
     
     
    Yes.. You can simply create a generic method and call that method in the respective activities and just passing the feld name as input parametr to fetch the value.
  • Friday, January 27, 2012 4:55 AM
     
     
    Actually I am very poor in C# oops concepts can you please explore how to do thanks in advance.
  • Friday, January 27, 2012 5:14 AM
     
     Answered Has Code
    private Collection<string> GetUserNames(string fieldName)
    {
        Collection<string> userNames = new Collection<string>();
        using (SPSite site = new SPSite("site url"))
        {
             using(SPWeb web = site.OpenWeb())
             {
    
                   SPList list = web.Lists["Departments"];
                   SPListItemCollection items = list.Items;
                    
                   foreach (SPListItem item in items)
                   {
                        string val =  item[fieldName].ToString();
                        userNames.Add(val);
                   }
              }
          }
          return userNames;
    }
    
    
    private void createTask1_MethodInvoking(object sender, EventArgs e)
            {
                          //Rest of your logic
                   Collection<string> hrNames = GetUserNames("HRName");
     
             }
    
    private void createTask2_MethodInvoking(object sender, EventArgs e)
            {
                          //Rest of your logic
                   Collection<string> dhNames= GetUserNames("DHName");
     
             }
    
    
     
    


    Modify the code as per your requirements.

     

  • Friday, January 27, 2012 5:20 AM
     
     Answered

    Thankyou very much for the code actually The Departments list having only one item. The list dont have many items.

    In the code you sent is for multilple items in Departments list I think .what I have to modify in that. Please help ..

     

  • Friday, January 27, 2012 5:26 AM
     
     

    Sorry Gitanjali..

    You sent the correct  thanks a ton..

  • Friday, January 27, 2012 5:28 AM
     
      Has Code
    private string GetUserName(string fieldName)
    {
        string userName = String.Empty;
        using (SPSite site = new SPSite("site url"))
        {
             using(SPWeb web = site.OpenWeb())
             {
    
                   SPList list = web.Lists["Departments"];
                   SPListItemCollection items = list.Items;
                    
                  SPListItem item = items[0];
                  SPFieldUser userField = (SPFieldUser)item.Fields.GetField(fieldName);
                            SPFieldUserValue userValue = (SPFieldUserValue)userField.GetFieldValue(item[fieldName].ToString());
                            SPUser user = userValue.User;
                            userName = user.LoginName
              }
          }
          return userName ;
    }
    
    
    private void createTask1_MethodInvoking(object sender, EventArgs e)
            {
                          //Rest of your logic
                   string hrName = GetUserName("HRName"); 
             }
    
    private void createTask2_MethodInvoking(object sender, EventArgs e)
            {
                          //Rest of your logic
                 string dhName = GetUserName("DHName"); 
             }
    
    

  • Friday, January 27, 2012 5:36 AM
     
     
    WOW what a code your geat Thanks a lot... Now I tested it is working good..Thanks once again Geethanjali
  • Friday, March 02, 2012 9:01 AM
     
     
    I have 2 textboxes and 1 submit button on my webpart when I click on save Button I need to save it a list called "TEST". I need to write that visual webpart code using MVP architecture. For that I have created 2 class librarys and one UI(Visual Webpart). I am new to MVP. kindly let me know what I need to write in each class and share the code .Thanks in advance
  • Friday, March 02, 2012 9:01 AM
     
     
    I have 2 textboxes and 1 submit button on my webpart when I click on save Button I need to save it a list called "TEST". I need to write that visual webpart code using MVP architecture. For that I have created 2 class librarys and one UI(Visual Webpart). I am new to MVP. kindly let me know what I need to write in each class and share the code .Thanks in advance
  • Monday, April 16, 2012 11:41 PM
     
     

    HI Geetanjali Arora I have a problem  with workflow

    the workflw should do this action

    person x send a request to his team leader when theteam leader make her decision( accepte  or refuse) the request sending automaticly to HR.

    it is necessary to concideration that we can found two person don't have the  same teamleader . so can  you help me please