Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Load ClientContext dynamically CSOM

$
0
0

Hello,

I am trying to implement request batching in my project. below is my scenario.

I want to bind data to 4 dropdownlists from 4 different lists. I understand that this can be achieved by loading the context 4 times and then executing the query. my question is how to load the context dynamically to create reusable function.

below is the code snippet i have tried so far:

 string sphosturl = (string)ViewState["sphosturl"];
            string accessToken = (string)ViewState["accesstoken"];
            IDxSPHelpers helpers = new SPHelpers();
            ClientContext clientContext = helpers.GetContext(sphosturl, accessToken);

            List xslt = clientContext.Web.Lists.GetByTitle("Xslt");
            List Variable = clientContext.Web.Lists.GetByTitle("Variable");

            CamlQuery camlQueryxsltList = new CamlQuery();
            camlQueryxsltList.ViewXml = "<Query><Where><Eq><FieldRef Name='DocIcon' /><Value Type='Computed'>xslt</Value></Eq></Where>" +"<OrderBy><FieldRef Name='FileLeafRef' Ascending='True' /></OrderBy></Query>";

            CamlQuery camlQueryValList = new CamlQuery();
            camlQueryValList.ViewXml = "<Query><Where><Eq><FieldRef Name='LinkFilename' /><Value Type='Computed'>{0}</Value></Eq></Where>" +"<OrderBy><FieldRef Name='FileLeafRef' Ascending='True' /></OrderBy></Query>";

            Microsoft.SharePoint.Client.ListItemCollection xsltItems = xslt.GetItems(camlQueryxsltList);
            Microsoft.SharePoint.Client.ListItemCollection ValListItems = Variable.GetItems(camlQueryValList);

            clientContext.Load(xsltItems);
            clientContext.Load(ValListItems);
            clientContext.ExecuteQuery();


            dtable = new DataTable();
            dtable.Columns.Add(new DataColumn("FileLeafRef"));

            foreach (var item in xsltItems)
            {
                DataRow dr = dtable.NewRow();
                foreach (var obj in item.FieldValues)
                {
                    if (obj.Key == "FileLeafRef")
                    {
                        dr[obj.Key] = obj.Value;
                    }
                }
                dtable.Rows.Add(dr);
            }

            ddlCustomStyleSheet.Items.Add("(Optional)");

            ddlCustomStyleSheet.DataSource = dtable;
            ddlCustomStyleSheet.DataTextField = "FileLeafRef";
            ddlCustomStyleSheet.DataValueField = "FileLeafRef";
            ddlCustomStyleSheet.DataBind();

            dtable = new DataTable();
            dtable.Columns.Add(new DataColumn("FileLeafRef"));

            foreach (var item in ValListItems)
            {
                DataRow dr = dtable.NewRow();
                foreach (var obj in item.FieldValues)
                {
                    if (obj.Key == "FileLeafRef")
                    {
                        dr[obj.Key] = obj.Value;
                    }
                }
                dtable.Rows.Add(dr);
            }

            ddlValFilter.Items.Add("(Optional)");
            ddlValFilter.DataTextField = "FileLeafRef";
            ddlValFilter.DataValueField = "FileLeafRef";
            ddlValFilter.DataSource = dtable;
            ddlValFilter.DataBind();

 


Thanks, Nilesh



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>