locked
Client Model - getting Property Bag value problem RRS feed

  • Question

  • Hi,

    I'm trying to develop a simple Silverlight application that I want to host in a SharePoint 2010 site. I'm using the SharePoint client model to communicate with SharePoint in the Silverlight app.
    I'm having problems with this. I'm getting an exception saying that the property doesn't exist.
    The code (SP being 'using SP = Microsoft.SharePoint.Client'):
                SP.ClientContext context;
                SP.Web web;
                SP.List list;
                string projectNr = null;
               
                context = SP.ClientContext.Current;
                web = context.Web;
                SP.PropertyValues propValues = web.AllProperties;

                context.Load(web);
                context.Load(propValues);
                // populate TextBlock with some stuff
                txtFields.Text = propValues["Projectnumber"];

    This doesn't work. I get a javascript error (created by the Silverlight app) with the exception:
    Webpage error details
    User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; GTB0.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; .NET4.0C; .NET4.0E)
    Timestamp: Mon, 15 Feb 2010 12:49:18 UTC
    Message: Unhandled Error in Silverlight Application 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.   at Microsoft.SharePoint.Client.PropertyValues.GetFieldValue(String fieldName)
       at Microsoft.SharePoint.Client.PropertyValues.get_Item(String fieldName)
       at InnovationPlatform.Silverlight.MainPage.LayoutRoot_Loaded(Object sender, RoutedEventArgs e)
       at System.Windows.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

    I know that the property by name 'Projectnumber' exists on the page, because it works using the SharePoint server object model.

    Does anyone know what I need to do to get the value of this property?
    Monday, February 15, 2010 1:04 PM

All replies

  • Did you find a solution for this? I'm facing the exact same problem on my site (SP2010 RTM version).
    MCTS in Web Application Development in .NET 2.0
    Thursday, April 29, 2010 11:59 AM
  • use this updated code to do the same..

    SP.ClientContext context;
                SP.Web web;
                SP.List list;
                string projectNr = null;
                
                context = SP.ClientContext.Current;
                web = context.Web;
                SP.PropertyValues propValues = web.AllProperties;

                context.Load(web);
                context.Load(propValues);

    use  context.ExecuteQuery(); 

                // populate TextBlock with some stuff
                txtFields.Text = propValues["Projectnumber"];

    you can also execute query in async way by calling context.ExecuteQueryAsync() method
    hope this will help you.

    Regards, Vikas Patel

    Decos Software Development Pvt. Ltd. (An ISO 9001:2008 Company)
    www.decos.in | www.decos.com
    Thursday, April 29, 2010 12:09 PM
  • You should read from the FieldValues in the PropertyValues. Below an asynchronic example.

    SP.ClientContext context;
    SP.Web web;           
    SP.List list;

    string projectNr = null;
                
    context = SP.ClientContext.Current;
    web = context.Web;
    SP.PropertyValues propValues = web.AllProperties;

    context.Load(web);
    context.Load(propValues);

    ----------------- MODIFIED FROM HERE -----------------

    clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);

    }

    private void onQuerySucceeded(object sender, ClientRequestSucceededEventArgs args)
    {
    Dispatcher.BeginInvoke(() => txtFields.Text = propertyValues.FieldValues["Projectnumber"].ToString());  
    }

    private void onQueryFailed(object sender, ClientRequestFailedEventArgs args)
    {
    Dispatcher.BeginInvoke(() => txtFields.Text = "Request failed");

    }

    Monday, April 11, 2011 2:31 PM
  • I did the above but it gives me this Exception , When I write :

    context.ExecuteQuery();

    or :

     clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);

     

    here is the Error :

     

    {System.InvalidOperationException: The method or property that is called may block the UI thread and it is not allowed. Please use background thread to invoke the method or property, for example, using System.Threading.ThreadPool.QueueUserWorkItem method to invoke the method or property.
       at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
       at SirverLightNotify.MainPage.TestSirverLight()
       at SirverLightNotify.MainPage..ctor()
       at SirverLightNotify.App.Application_Startup(Object sender, StartupEventArgs e)
       at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
       at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)}


    thank u for help
    Thursday, January 19, 2012 4:26 AM