View a site column using silverlight? Is it possible?
-
lundi 21 mars 2011 11:02
Hi!
I have a site column (called 'Product', of type Choice so it can be used site wide for lots of stuff) and I want to look up the values in this column in a silverlight app that will be a web app on a sharepoint page.
I couldn't find any documentation that described how to do this, so I decided to do it a different way and create a list (called "All Products") with the 'Product' column in it to essentially be a list that mirrors the products (in the 'Product' site-wide column).
I have tried to write the code to look up the values of the column and insert them into a combo box but I can't do it! I initially tried to get the list, use a CAML query to select the Products, and then iterate through the results, but it didn't work. So then I tried to bind the combobox's data source to the list returned from the CAML query, which results in the correct NUMBER of items being bound to the combobox, but they're all called Microsoft.Sharepoint.Client.ListItem! I looked through the documentation but it seems that there is no way to get the values out of the ListItem using the Sharepoint/Silverlight Object model? If someone could point me in the right direction that would be awesome!
Here is the code that gets me the closest:
Thanks!private void UserControl_Loaded(object sender, RoutedEventArgs e) { ClientContext spContext = new ClientContext("http://sharepoint/projects/"); spContext.Load(spContext.Web); productList = spContext.Web.Lists.GetByTitle("All Products"); spContext.Load(productList); CamlQuery query = new CamlQuery(); string strQuery = "<Query><Where><IsNotNull><FieldRef Name='Product' /></IsNotNull></Where></Query>"; query.ViewXml = strQuery; _allProducts = productList.GetItems(query); spContext.Load(_allProducts); spContext.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(listLoadSucceeded), new ClientRequestFailedEventHandler(listLoadFailed)); } private void listLoadSucceeded(Object sender, ClientRequestSucceededEventArgs args) { Dispatcher.BeginInvoke(() => { /* The commented out code below does not work! foreach (ListItem li in _allProducts) { comboProduct.Items.Add(li["Product"].ToString()); } */ comboProduct.ItemsSource = _allProducts.ToList(); }); }
Toutes les réponses
-
mardi 22 mars 2011 13:53
foreach (ListItem li in _allProducts)
{
comboProduct.Items.Add(li["Product"].ToString());
}
This may have not worked because you must check to make sure li["Product"] is not null before calling ToString()
Blog | SharePoint Field Notes Dev Tool | ClassMaster- Marqué comme réponse Xue-Mei Chang-MSFTModerator mardi 29 mars 2011 01:28
-
samedi 14 avril 2012 01:54
Hi But it crashes when i Execute, at this
clientContext.ExecuteQueryAsync(onTermTypeSucceeded, onQueryFailed);
The Whole Code follows
List listTermType = clientContext.Web.Lists.GetByTitle("HospitalTermType");
clientContext.Load(listTermType);
CamlQuery query = new CamlQuery();
string strQuery = "<Query><Where><IsNotNull><FieldRef Name='TermType' /></IsNotNull></Where></Query>";
query.ViewXml = strQuery;
allTermTypes = listTermType.GetItems(query);
clientContext.Load(allTermTypes);
MessageBox.Show("Before Exe");
clientContext.ExecuteQueryAsync(onTermTypeSucceeded, onQueryFailed);
}private void onTermTypeSucceeded(object sender, ClientRequestSucceededEventArgs args)
{
Deployment.Current.Dispatcher.BeginInvoke(DisplayTermTypes);
}private void DisplayTermTypes()
{
foreach (ListItem termType in allTermTypes)
{
if (termType["TermType"] != null)
ComboBoxTermType.Items.Add(termType["TermType"].ToString());
}
}

