4

I want to return all info for a specified user. The internal field name is UserAssigned. If I use this

foreach (ListItem listItem in listItemCollection)
{
  Console.WriteLine(listItem[“UserAssigned”]);
}


I get Microsoft.SharePoint.Client.FieldUserValue returned. How can I translate that to either set my CAML query to return everything for ID 31836 OR username Jones, Jack?

This is C# and CSOM

EDIT
This is the syntax I have...

string siteURL = "";
var ctx = new ClientContext(siteURL);
var web = ctx.Web;
List list = web.Lists.GetByTitle("ListTitle");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = "";

var listItemCollection = list.GetItems(camlQuery);

ctx.Load(listItemCollection,
         eachItem => eachItem.Include(
                     item => item,
                     item => item["ID"],
                     item => item["UserAssigned"]
                     ));

ctx.ExecuteQuery();

foreach (ListItem listItem in listItemCollection)
{
    Console.WriteLine(listItem["ID"];
    FieldUserValue un = (FieldUserValue)listItem["UserAssigned"];
    string unGood = un.LookupValue;
    Console.WriteLine(unGood);
}
Console.ReadLine();

Edit 2
I edited my syntax to be the below - but it is ignoring my where clause...

User user = ctx.Web.CurrentUser;
ctx.Load(user);
ctx.ExecuteQuery();
string itsme = user.Title;

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = string.Format("<Where><Eq><FieldRef Name='UserAssigned' /><value Type='User'>{0}</Value></Eq></Where>", itsme);
var listItemCollection = list.GetItems(camlQuery);

ctx.Load(listItemCollection, 
         eachItem => eachItem.Include(
                 item => item,
                 item => item["ID"],
                 item => item["UserAssigned"]
                 ));

ctx.ExecuteQuery();

foreach (ListItem listItem in listItemCollection)
{
  Console.WriteLine(listItem["ID"];
  FieldUserValue un = (FieldUserValue)listItem["UserAssigned"];
  string unGood = un.LookupValue;
  Console.WriteLine(unGood);
}
Console.ReadLine();

2 Answers 2

5

To get the display name of the user from the listItem["UserAssigned"]

FieldUserValue user = (FieldUserValue)listItem["UserAssigned"];
string name = user.LookupValue; //display name

CAML query by this user

string query=string.Format("<View><Query><Where><Eq><FieldRef Name='UserAssigned'/><Value Type='User'>{0}</Value></Eq></Where></Query></View>", name );

Edit

to get the current logged in user

User user = ctx.Web.CurrentUser;    
ctx.Load(user);
ctx.ExecuteQuery();
string name = user.Title;

You can query one time using the current logged in user using the above CAML query

19
  • see my edit - that shows how to parse out the required info AFTER a CamlQuery has been executed. So would I then need to run a second CamlQuery to get the exclusive data for the username I am after? Ore am I missing the obvious here? Apr 11, 2018 at 19:20
  • @user2676140 will the first part of my answer is getting the user info , which you already did, the second part of my answer showing how to query using user's name (string name) in the CAML query
    – A.Saéed
    Apr 11, 2018 at 19:30
  • If I understand correctly, then I would then need to add in a second CamlQuery to then exclusively pull the data for the UserAssigned? Apr 11, 2018 at 19:43
  • I am wanting to only pull all records for ONE user - would I have to 1) Query all records to get unique names, then 2) Query sharepoint again to query against one name? Apr 11, 2018 at 19:50
  • 1
    @user2676140 user.Title; will return the name user.LoginName; will return cred used in log in to sharepoint
    – A.Saéed
    Apr 11, 2018 at 20:59
1

You do your query call before you pull the list items. Once you have your query then you get your list items based off of that.

Example below:

using (SPSite spSite = new SPSite(SPContext.Current.Web.Url))
{
    using (SPWeb spWeb = spSite.OpenWeb())
    {
        SPQuery query = new SPQuery();
        //Get your item by ID then pull what you want after.  This query returns a single list item from your ID
        query.ViewXml = string.Concat("<View><Query><Where><Eq><FieldName Name='ID' /><Value Type='Integer'>31836</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>");

        string listUrl = spWeb.ServerRelativeUrl + "/Lists/YourList";
        SPList list = spWeb.GetList(listUrl);
        SPListItemCollection items = list.GetItems(query);

        foreach(SPListItem item in items) {
            //here is your stuff for that user (List Item)
        }
    }
}

I think this is what you are looking for. Hope this helps.

4
  • User assigned.ID = 31836 and userassigned.name = Jones, Jack. Unless I parse out the data I get my error described above. How can I at the CAML query portion extract out the value for name or id to use in my query? If I attempt the sample you suggest above, no results are returned bc it always evaluated to false. Apr 11, 2018 at 16:43
  • paste your code. I do not think you understand what this function is doing. This is a SPOM replacement which uses your id and will retrun a single result. This code works.
    – lazoDev
    Apr 11, 2018 at 16:51
  • I am mobile atm, but will post my code soon. I do not want a single result returned. I am after all data belonging to a specific userID or username. Apr 11, 2018 at 16:58
  • I think I missunderstood the question. I do that sometimes. Someone below me posted something that look at.
    – lazoDev
    Apr 11, 2018 at 17:09

Your Answer

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

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