locked
Set a SPLIstItem with First name and last name RRS feed

  • Question

  • I have got the first name, last name and email of a person.

    Is it possible to fill the SPListIistItem? I guess I should use SPFieldUserValue but I do not know if there is a way to retrieve the login name of the user.

    Thursday, March 28, 2013 3:57 PM

Answers

  • You can use SPUtility.ResolvePrincipal method. It returns SPPrincipalInfo object using display name or email address of the user. The principal info object has the login ID which you can use SPWeb.EnsureUser. This will return the SPUser object. 

    You can play around...I am not sure but I read it somewhere that People or Group column behaves differently in single-valued and multi-valued scenario. So first check what is the type of value this field uses. Is it a SPUser or SPFieldUserValue or string?  e.g. item["Created_x0020_By"].GetType().Name.

    Remove the statement after checking the type.


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    • Marked as answer by okdreamy Tuesday, April 2, 2013 6:45 AM
    Thursday, March 28, 2013 5:07 PM

All replies

  • You can use SPUtility.ResolvePrincipal method. It returns SPPrincipalInfo object using display name or email address of the user. The principal info object has the login ID which you can use SPWeb.EnsureUser. This will return the SPUser object. 

    You can play around...I am not sure but I read it somewhere that People or Group column behaves differently in single-valued and multi-valued scenario. So first check what is the type of value this field uses. Is it a SPUser or SPFieldUserValue or string?  e.g. item["Created_x0020_By"].GetType().Name.

    Remove the statement after checking the type.


    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    • Marked as answer by okdreamy Tuesday, April 2, 2013 6:45 AM
    Thursday, March 28, 2013 5:07 PM
  • Login name of the current user can be retrieved using javascript. add a CEWP to your page and post ur javascript there.

    <script language="ecmascript" type="text/ecmascript">

        var clientContext = null;

        var oWeb = null;

        var user = null;

        function getUserInfo() {

            clientContext = new SP.ClientContext.get_current()

            oWeb = clientContext.get_web();

            user = oWeb.get_currentUser();

            clientContext.load(user);

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));

        }

        function onQuerySucceeded() {

            var message = "User Information";

            message += "\n User Name: " + user.get_title();

            message += "\n Login Name: " + user.get_loginName();       

            alert(message);

        }

        function onQueryFailed(sender, args) {

            alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());

        }

    </script>

    Hope this helps

    Friday, March 29, 2013 10:07 AM
  • Thanks Pradip, it works fine. Do I have to use EnsureUser? my code looks like this

    SPPrincipalInfo userCustomerInfo = SPUtility.ResolvePrincipal(workflowProperties.Web,        customer.email,                            SPPrincipalType.All, SPPrincipalSource.All, null, true);
    if (userCustomerInfo != null)
    {
    workflowProperties.Item[Fields.ItemCustomer] = new SPFieldUserValue(workflowProperties.Web,userCustomerInfo.
    PrincipalId,userCustomerInfo.DisplayName);
    }

    Tuesday, April 2, 2013 6:48 AM
  • As you mentioned that you have first name, last name and email of the user, I suggested to directly use ResolvePrincipal. But if you are not sure that the user is valid or not, then use EnsureUser also.

    Pradip T. ------------- MCTS(SharePoint 2010/Web)|MCPD(Web Development) https://www.mcpvirtualbusinesscard.com/VBCServer/paddytakate/profile

    Tuesday, April 2, 2013 7:03 AM