How To Insert FieldUser Value in Sharepoint using silverlight(Client object model)

How To Insert FieldUser Value in Sharepoint using silverlight

use following code to insert fielduser value in sharepoint

C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint;

namespace users
{


public partial class MainPage : UserControl
{
string user;
GroupCollection Groupcoll;
ClientContext site;

List listobj;
User username               //This one is for the field user name 
 
public  MainPage()
{



InitializeComponent();
site = new ClientContext(ApplicationContext.Current.Url);

site.Load(site.Web);
listobj = cc.Web.Lists.GetByTitle("your list name");
cc.Load(listobj);
 
Groupcoll= site.Web.SiteGroups;
site.Load(Groupcoll, groups => groups.Include(group => group.Users));

site.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), null);
}

private void OnRequestSucceeded(Object sender, ClientRequestSucceededEventArgs args)
{

Dispatcher.BeginInvoke(datacon);
}



private void datacon()
{
    //To load all sharepoint user in combobox first
    foreach (Group Grp in Groupcoll)
    {
     UserCollection collUser = Grp.Users;
     foreach (User Usr in collUser)
    {
     user = Convert.ToString(Usr.Title);
     combobox1.Items.Add(user);
    }
    }
     //To Insert Selected Combobox user to Sharepoint list
    ListItem litem = listobj.AddItem(new ListItemCreationInformation());
    foreach (Group grp in Groupcoll)
                     {
                         UserCollection _usrcoll = grp.Users;
                         foreach (User usr in _usrcoll)
                         {
                             if (usr.Title ==
combobox1.SelectionBoxItem.ToString())
                             {
                                 username= usr as User;
                             }

                         }
                     }
                    
    litem["Column Name"] = username;
    litem.Update();
    cc.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(succed), null);
} 
private void succed(object sender, ClientRequestSucceededEventArgs arg)
 {
 } 


If you want to add multiple FieldUser Value then:

check this link

Assign an list of FieldUserValue if you need to assign more than one user to the field.
    string[] users = { "domain\\johndoe", "domain\\peterpan" };
    var projectMembers = users
        .Select(loginName => FieldUserValue.FromUser(loginName))
        .ToList();

    litem["Column Name"] = projectMembers;
    litem.Update();

Comments

Popular posts from this blog

Upload Single/Multiple file by using the REST API and jQuery SharePoint 2013

Add content type to SharePoint List/Library using REST API

A type named 'SP.Data. could not be resolved by the model error