none
ERROR - The property or field has not been initialized.

    Question

  • Hi all,

    I am working on my first attempt at using SharePoint 2010 in a Silverlight app.  I want to be able to display and insert/update list items in a listbox (for now) in the Silverlight app.  When I run the app, I see no errors but get no data either.  When I walk through the code I get the following exception:

    Message = "The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested."  on this line

    oFavoritesList = context.Web.Lists.GetByTitle(

     

    "FavoritesList");

    Including all of the code since there isn't much yet.  What am I missing?

    public partial class MainPage : UserControl
      {
        public List oFavoritesList;
        public ListItemCollection collListItem;
    
        private static string websiteUrl = "http://localhost";
    
        private ClientContext context = new ClientContext(websiteUrl);
       
        public MainPage()
        {
          InitializeComponent();
    
          oFavoritesList = context.Web.Lists.GetByTitle("FavoritesList");
    
          context.Load(oFavoritesList);
    
          CamlQuery camlQuery = new CamlQuery();
          System.Text.StringBuilder oSb = new System.Text.StringBuilder();
    
          oSb.Append("   <View Name=\"{D93583AC-EB29-46CE-97DF-F1A9DB19E568}\" DefaultView=\"TRUE\" MobileView=\"TRUE\" MobileDefaultView=\"TRUE\" Type=\"HTML\" DisplayName=\"All Items\" Url=\"Lists/FavoritesList/AllItems.aspx\" Level=\"1\" BaseViewID=\"1\" ContentTypeID=\"0x\" ImageUrl=\"/_layouts/images/generic.png\" xmlns=\"http://schemas.microsoft.com/sharepoint/soap/\">");
          oSb.Append("     <Query>");
          oSb.Append("       <OrderBy>");
          oSb.Append("          <FieldRef Name=\"ID\" />");
          oSb.Append("       </OrderBy>");
          oSb.Append("     </Query>");
          oSb.Append("     <ViewFields>");
          oSb.Append("       <FieldRef Name=\"Alias\" />");
          oSb.Append("       <FieldRef Name=\"ReportName\" />");
          oSb.Append("       <FieldRef Name=\"URL\" />");
          oSb.Append("     </ViewFields>");
          oSb.Append("     <RowLimit Paged=\"TRUE\">30</RowLimit>");
          oSb.Append("  </View>");
    
          string sResult = oSb.ToString();
    
          camlQuery.ViewXml = sResult;
           
          //camlQuery.ViewXml = "<View><Query><Where><IsNotNull><FieldRef Name='Title'/></IsNotNull></Where></Query></View>";
          
          collListItem = oFavoritesList.GetItems(camlQuery);
    
          context.Load(collListItem,
          items => items.Include(
          item => item.Id,
          item => item["Alias"],
          item => item["ReportName"],
          item => item["URL"]));
    
          ClientRequestSucceededEventHandler success = new ClientRequestSucceededEventHandler(SuccesHandler);
          ClientRequestFailedEventHandler failure = new ClientRequestFailedEventHandler(FailureHandler);
          context.ExecuteQueryAsync(success, failure);
         
        }
    
        private void SuccesHandler(object Sender, ClientRequestSucceededEventArgs e)
        {
          List<Favorites> favs = new List<Favorites>();
          foreach (ListItem item in collListItem)
          {
            Favorites fav = new Favorites(item["Title"].ToString(), item["Alias"].ToString(), item["ReportName"].ToString(), item["URL"].ToString());
            favs.Add(fav);
          }
    
          listBox1.ItemsSource = favs;
        }
    
        private void FailureHandler(object Sender, ClientRequestFailedEventArgs e)
        {
          
        }

    Thanks in advance for any help you can give me. 

    Rhonda Bailey

    Friday, June 04, 2010 8:15 PM

Answers

  • I actually found an easier way to do this that seems to work quite well.  Service reference to lists.asmx and then the following code:
    ListsSoapClient proxy = new ListsSoapClient();
          proxy.GetListItemsCompleted += new
            EventHandler<GetListItemsCompletedEventArgs>(proxy_GetListItemsCompleted);
    
          //Define the parameters for the service call
          XElement query = new XElement("Query");
          XElement queryOptions = new XElement("QueryOptions");
          XElement viewFields = new XElement("ViewFields");
    
         
          proxy.GetListItemsAsync("FavoritesList",
            null,
            query,
            viewFields,
            null,
            queryOptions,
            null);
        }
    
        
    
        void proxy_GetListItemsCompleted(object sender, GetListItemsCompletedEventArgs e)
        {
          XNamespace ns = "#RowsetSchema";
    
          var query = from x in e.Result.Descendants()
               where x.Name == ns + "row" && x.Attribute("ows_Alias").Value.ToLower() == "rhonda"
          select x.Attribute("ows_URL").Value;
    
          myList.ItemsSource = query;
        }

     

    Rhonda

    • Marked as answer by Chengyi Wu Tuesday, June 08, 2010 2:34 AM
    Monday, June 07, 2010 6:03 PM

All replies

  • Please split the following statement to know which property access is throwing exception.

    context.Web.Lists.GetByTitle("FavoritesList");

    You need to execute the query after the following line to make sure the list is loaded:

    context.Load(oFavoritesList);

     

     


    Thanks,
    Sohel Rana
    http://ranaictiu-technicalblog.blogspot.com
    Monday, June 07, 2010 8:44 AM
  • I actually found an easier way to do this that seems to work quite well.  Service reference to lists.asmx and then the following code:
    ListsSoapClient proxy = new ListsSoapClient();
          proxy.GetListItemsCompleted += new
            EventHandler<GetListItemsCompletedEventArgs>(proxy_GetListItemsCompleted);
    
          //Define the parameters for the service call
          XElement query = new XElement("Query");
          XElement queryOptions = new XElement("QueryOptions");
          XElement viewFields = new XElement("ViewFields");
    
         
          proxy.GetListItemsAsync("FavoritesList",
            null,
            query,
            viewFields,
            null,
            queryOptions,
            null);
        }
    
        
    
        void proxy_GetListItemsCompleted(object sender, GetListItemsCompletedEventArgs e)
        {
          XNamespace ns = "#RowsetSchema";
    
          var query = from x in e.Result.Descendants()
               where x.Name == ns + "row" && x.Attribute("ows_Alias").Value.ToLower() == "rhonda"
          select x.Attribute("ows_URL").Value;
    
          myList.ItemsSource = query;
        }

     

    Rhonda

    • Marked as answer by Chengyi Wu Tuesday, June 08, 2010 2:34 AM
    Monday, June 07, 2010 6:03 PM