Add new item with current User [Silverlight, SharePoint 2010]

已答复 Add new item with current User [Silverlight, SharePoint 2010]

  • 01 Mei 2012 14:12
     
      Memiliki Kode

    Hi Everybody,

    I am created a Silverlight application which will add items in a SharePoint list. (MVVM framework: Caliburn.Micro) In the list we have several fields including a field named: Employee. The type of the field is a people picker. When the user clicks save, the data in the Silverlight application need to be stored in the list. The data of the Employee field needs to be the current user.

    When the application starts I retrieve the current user with the following snipped:

    public IEnumerable<IResult> RetrieveCurrentUser()
    {
        SP.ClientContext context = new SP.ClientContext(App.SharePointUrl);
        SP.Web web = context.Web;
        SP.User user = web.CurrentUser;
                
        context.Load(user);
        yield return new Common.SPClientRequestResult(ref context,
            (e) => { User = user; },
            (e) => { throw e.Exception; });
    }

    This seems to work well. When I watch the User property it is showing me the correct information about the current user (LoginName, display name etc).

    The problem (challenge) begins when I try the save an item with the current user. It is giving me the following Exception:

    The given key was not present in the dictionary.

       at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
       at Microsoft.SharePoint.Client.ClientRequest.BuildQuery()
       at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery(ClientRequestSucceededEventHandler succeededCallback, ClientRequestFailedEventHandler failedCallback)
       at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQueryAsync(ClientRequest request, ClientRequestSucceededEventHandler succeededCallback, ClientRequestFailedEventHandler failedCallback)

    I use the following code sniped for creating the list item:

    public IEnumerable<IResult> CreateBookHours(BookHourModel model)
    {
        SP.ClientContext context = new SP.ClientContext(App.SharePointUrl);
        SP.Web web = context.Web;
        SP.List list = web.Lists.GetByTitle("Booked Hours");
        // add following line for debugging
        context.Load(User);
        // add following line for debugging
        yield return new Common.SPClientRequestResult(ref context, (e) => { }, (e) => { });
    
        SP.ListItem item = list.AddItem(new SP.ListItemCreationInformation());
        item["Title"] = model.Title;
        item["Employee"] = User;
        item.Update();
    
        yield return new Common.SPClientRequestResult(ref context,
            (e) =>
            {
                model.Id = item.Id;
                model.Saved();
            },
            (e) => { throw e.Exception; },
            () => { context.Dispose(); });
    }

    When I manually add an item into the "Booked Hours" list (in SharePoint) and pick the current user it is working. My Silverlight App it even showing me the list items from the current user without a problem.

    Who can help me?

    Rick

Semua Balasan

  • 01 Mei 2012 15:51
     
     Jawab Memiliki Kode

    Try assigning the ID of the employee instead:

    item["Employee"] = User.Id;

    Ruud

  • 01 Mei 2012 15:55
     
     
    That seems to work.... Very strange...