Wednesday, August 24, 2011

SharePoint 2010 Managed Client Object Model. How to Get Site Properties?


Silverlight applications use this Managed Client Object Model .In the Client Object Model, each line of statement that requires some site information is a request to the server, the server responds to the request and the Response is packed into a Client Object which is easy to use. In Client Object Model we cannot get all the required data on a single request as the data is too big SharePoint uses On Demand serve request methodology for the Client Object Model. The developer should take care when getting data from the server.He should request minimal information he needs from the Server using LINQ filter queries.

Below is the code to Get Site Properties.

PropertyValues pValues = null;
string siteCollectionUrl = string.Empty;  // stores sitecollection url;
string siteUrl = http://yoursitename ; // site url, may be site under a sitecollection;
string PropertyIDValue = string.Empty;



private void LoadSite()
        {
            ClientContext ctx = new ClientContext(siteUrl);
            siteCollection = ctx.Site;                     
            ctx.Load(siteCollection, s => s.Url);
            ctx.ExecuteQueryAsync(GetSiteRequestSucceeded, RequestFailed);
        }

        private void GetSiteRequestSucceeded(object sender, SP.ClientRequestSucceededEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                siteCollectionUrl = siteCollection.Url;
                ClientContext ctx = new ClientContext(siteUrl)
                currentWeb = ctx.Web;
                ctx.Load(currentWeb, w => w.Title, w => w.ServerRelativeUrl, w => w.AllProperties);
                ctx.ExecuteQueryAsync(GetWebRequestSucceeded, RequestFailed);
            });
        }

        private void GetWebRequestSucceeded(object sender, SP.ClientRequestSucceededEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                pValues = currentWeb.AllProperties;
         ClientContext ctx = new ClientContext(siteUrl) ;
         ctx.Load(pValues);
                context.ExecuteQueryAsync(GetPropsSuccess, RequestFailed);
                }
            });
        }

        private void GetPropsSuccess(object sender, SP.ClientRequestSucceededEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
              this.PropertyIDValue = (string)pValues.FieldValues["propertyid"];
             
            });
        }

       

private void RequestFailed(object sender, SP.ClientRequestFailedEventArgs e)
        {
            Dispatcher.BeginInvoke(() =>
            {
                MessageBox.Show("Error occured:  " + e.Exception);
            });
        }