How to get item value under one column in a list using sharepoint object model
-
Thursday, January 26, 2012 2:50 PMI 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
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
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- Proposed As Answer by satish gorijala Thursday, January 26, 2012 3:51 PM
- Marked As Answer by Rajashekar Indoori Thursday, January 26, 2012 3:51 PM
-
Thursday, January 26, 2012 4:22 PMHow can we do if it the list having multiple items?
-
Thursday, January 26, 2012 4:27 PM
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
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.
- Marked As Answer by Rajashekar Indoori Thursday, January 26, 2012 7:11 PM
-
Thursday, January 26, 2012 7:11 PMYou are great!!!! I hard coded the URL then its start working THANKYOU VERY MUCH...THANKS FOR YOUR HELP
-
Thursday, January 26, 2012 7:13 PMGlad 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 AMYes.. 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 AMActually 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
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.- Marked As Answer by Rajashekar Indoori Friday, January 27, 2012 5:26 AM
-
Friday, January 27, 2012 5:20 AM
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 ..
- Marked As Answer by Rajashekar Indoori Friday, January 27, 2012 5:25 AM
-
Friday, January 27, 2012 5:26 AM
Sorry Gitanjali..
You sent the correct thanks a ton..
-
Friday, January 27, 2012 5:28 AM
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 AMWOW 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 AMI 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 AMI 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

