Solved

.NET CSOM

Posted on 2013-11-13
1
521 Views
Last Modified: 2013-12-11
1)  I created the below windows form and .NET Managed client object code.  It is supposed to retrieve items from a “Projects” list based on my CAML query and render the returned items into a listbox.  It works fine but everything gets returned.  The filter is not really working.  Any ideas?  
Here’s my form:  
Form
Code:
using (ClientContext ctx = new ClientContext("http://isaac.issharepoint.com/examples/"))
            {
                ctx.Credentials = new NetworkCredential("guest1", "xxxxxx#", "Sharepointroad");
                Site site = ctx.Site; //Loads site collection into the site object
                ctx.Load(site);

                Web wb = site.RootWeb;
                ctx.Load(wb);

                ctx.ExecuteQuery(); //Go to the server and run all that has been loaded

                //listBox1.Items.Add(ctx.Url);
                //listBox1.Items.Add(site.Url);

                List lst = wb.Lists.GetByTitle("Projects");
                ctx.Load(lst);

                ctx.ExecuteQuery();
                listBox1.Items.Add(lst.Title);

                CamlQuery query = new CamlQuery();
                query.ViewXml = "<Query><Where><Eq><FieldRef Name='ProjNumber' /><Value Type='Text'>"+proj.Text.ToString()+"</Value></Eq></Where></Query>";

                ListItemCollection all = lst.GetItems(query);
                ctx.Load(all);
                ctx.ExecuteQuery();

                foreach (ListItem myList in all)
                {
                    listBox1.Items.Add("Project Number: " + myList["ProjNumber"]);
                    listBox1.Items.Add("       Amount Projected: " + myList["AmtProjected"]);
                    listBox1.Items.Add("       Fiscal Year: "+myList["FiscalYear"]);
                }
            }

Open in new window


2)  How do I change “Web wb = site.RootWeb;” to really point to http://isaac.issharepoint.com/examples/.
I don’t want the root level “Projects” list.  I want the “Projects” in the “examples” site.  Yesterday, I kept getting an error and it went away when I created a “Projects” list at site collection level which is not the list I want.

Any ideas?  Thanks for any help you can provide.
0
Comment
Question by:Isaac
1 Comment
 
LVL 44

Accepted Solution

by:
Rainer Jeschor earned 500 total points
ID: 39647875
Hi,

sorry-have not yet had the time to prototype / verify the code, but from what I see it should be:

1. Error in your CAML query
You missed the "VIEW" root element in your string.
So instead of
query.ViewXml = "<Query><Where><Eq><FieldRef Name='ProjNumber' /><Value Type='Text'>"+proj.Text.ToString()+"</Value></Eq></Where></Query>";

Open in new window

it should be
query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='ProjNumber' /><Value Type='Text'>"+proj.Text.ToString()+"</Value></Eq></Where></Query></View>";

Open in new window


2. Subsite
It seems that examples is a sub site and not a site collection. Therefore your code
 Web wb = site.RootWeb;

Open in new window

will simply get the site collections root web - which is "/" (in your case http://isaac.issharepoint.com/).
Please replace the above line with
Web wb = site.OpenWeb("/examples");

Open in new window


HTH
Rainer
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

We were having a lot of "Heartbeat Alerts" in our SCOM environment, now "Heartbeat" in a SCOM environment for those of you who might not be familiar with SCOM is a packet of data sent from the agent to the management server on a regular basis, basic…
User Beware!  This is a rather permanent solution to removing your email from an exchange server.  The only way to truly go back is to have your exchange administrator restore your mailbox from backups.  This is usually the option of last resort.  A…
Viewers will learn how to maximize accessibility options in an Excel workbook for users with accessibility issues.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now