Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 567
  • Last Modified:

.NET CSOM

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
Isaac
Asked:
Isaac
1 Solution
 
Rainer JeschorCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now