Reading/writing list items using SharePhone

Ok, so, a quick example how to load in all items in a list and update a property, then save it back to SharePoint:

list.Items.ItemsLoaded += (object se, EventArgs ev) => {
    BaseItem item = list.Items[0];
    item.Title = "NewTitle";
    item.Update();
    list.Items.Update((object obj, EventArgs ea) => {
        
    });
};
list.Items.LoadAllItems();

There’s a few things going on here. As this works asynchronously, we add a event handler which will run when the items have been loaded into the list then we take the first item and updates the title before saving it back.

Whenever updating an item, be sure to call Update() on the item afterwards to tag it for updating. This is yet again a way to ensure that only required data is transferred, hopefully making the payload smaller. When all updates are ready, call the Update() method on the item collection (list.Items.Update(.. )).

The latter update method requires a callback delegate which will be called when the update is complete – this may change in future releases.

Remember, these properties can be databound to your WP7 controls.

If you don’t want to load the entire item collection, you can specify which items you want loaded using a CAML query.

CamlExpression titleQuery = ExpressionFactory.CreateExpression("Title", SharePhone.Query.Expression.FieldType.Text, SharePhone.Query.Expression.Operator.Eq, "NewTitle");
list.GetItemsByQuery(titleQuery, 0, (BaseItemList<BaseItem> items) => {
    if (items.Count > 0) {
        //query successfull
    }
});

The CAML builder classes in SharePhone lets you build queries rather easily, like for example using JOINs:

CamlExpression titleQuery = ExpressionFactory.CreateExpression("Title", SharePhone.Query.Expression.FieldType.Text, SharePhone.Query.Expression.Operator.Eq, "NewTitle");
CamlExpression dateFilter = ExpressionFactory.CreateExpression("PublishedDate", SharePhone.Query.Expression.FieldType.DateTimeWithTimeIncluded, SharePhone.Query.Expression.Operator.Geq, DateTime.Now);
CamlExpression query = ExpressionFactory.JoinExpression(SharePhone.Query.Expression.Conjunction.And, new CamlExpression[] { titleQuery, dateFilter });
list.GetItemsByQuery(query, 0, (BaseItemList<BaseItem> items) => {
    if (items.Count > 0) {
        //query successfull
    }
});

Advertisements

2 Responses to Reading/writing list items using SharePhone

  1. Mitchell says:

    I can’t figure out how you are getting “list”. The best I can do is get the GetItemsByName command.

    • Frode Hus says:

      It depends what you mean by “list” – the object or the list name?
      All available lists can be retrieved using the Lists property on a SPWeb

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: