Select to view content in your preferred language

Querying a sharePoint List

3390
7
12-13-2010 10:27 AM
HeatherHainsworth
Deactivated User
Hi,

I am trying to query a share point list in a ArcGIS Web Part. So I took the sample on line from the SDK and changed it so that it was pointing to my list and my list fields and I can't seem to get it to work.

I have put pop up windows in the code and it never seems to go to the on failure or on success functions it dies at
clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);

My X and Y fields I am using are number fields (I have also tried to use the ShapeX and ShapeY fields from a ArcGIS Geocode Workflow) and nothing seems to work.

I have taken the sample as is with the cities list and I have gotten the query by attribute to work that way.

  private void StateComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ClientContext clientContext = ClientContext.Current;
            Web web = clientContext.Web;

            // Query the list "Cities" with the selected state name
            List citiesList = web.Lists.GetByTitle("Test"); // My lists name is Test
            CamlQuery camlQuery = new CamlQuery();
            string stateName = StateComboBox.SelectedValue.ToString();
            camlQuery.ViewXml =
            @"<View>
                <Query>
                  <Where>
                    <Eq>
                      <FieldRef Name='Title'/>
                      <Value Type='Text'>" + stateName + @"</Value>
                    </Eq>
                  </Where>
                </Query>
                <RowLimit>1000</RowLimit>
              </View>";

            citiesListItems = citiesList.GetItems(camlQuery);
            clientContext.Load(citiesList, list => list.Title);
            clientContext.Load(citiesListItems, items => items.Include(
                item => item["X"],
                item => item["Y"]
                ));

            MapApplication.Current.ShowWindow("Ready to Execute", new TextBlock()
            {
                Text = "Ready to Execute. ",
                TextWrapping = TextWrapping.Wrap,
                Margin = new Thickness(30),
                MaxWidth = 480
            });

            clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
        }
0 Kudos
7 Replies
RichZwaap
Frequent Contributor
Hi Heather:

Your code looks fine.  Do the field names match what you have in your "Test" list?  The CAML assumes that the state name is stored in a field called "Title," and the items.Include statement specifies field names of X and Y.

If the field names check out and you still have problems, I would recommend putting together a quick Silverlight app that does nothing other than querying your SharePoint list.  Once you get that working, then you can move it into your command implementation.
0 Kudos
HeatherHainsworth
Deactivated User
Well I found a few things out, first I had taken the SDK Sample and just changed the names of the namespaces and XAP file which I have done on other silverlight applicaitons and I have had no issues with it but I must have missed a reference somewhere. Because I created the command from a blank silverlight application and I have it querying now.

I also learned that if you rename the Title Field (a default field with sharepoint) you still have to refer to it as Title in the application.

So now I have it querying and returning results and plotting on the map. It returns the Name (Title), Address, and City.

I tried to add 3 other fields and it fails again. Sois there any requirement for the types of fields you can return in the query?

I tried number fields, fields without spaces and with spaces, and I tried converting Number fields to single text and I still can't get the query to return results when I add those fields....
0 Kudos
RichZwaap
Frequent Contributor
Hi Heather:

There are two things to be aware of here about the CAML query.  First, you need to specify the internal field name of the fields you wish to return.  The internal name is set at the time the column is created and never changes, even when you update the field's display name.  Columns that are automatically generated by SharePoint, such as the Title field, often have special names.  In other cases, some characters in the display name are substituted in the internal name.  One example is spaces, which are replaced with "_x0020_" in the internal name.

One way to see the internal name of the field is to navigate to the list, sort on the field name you wish to know by clicking on that field's header, then look at the URL in the browser's address bar.  Included in the URL you will see an "&SortField=" parameter.  That will tell you the internal name is.

Second, if you are specifying a Value in the CAML, you need to correctly specify the Type attribute.  So in the example, the type is specified as "Text."

While it's good to know how CAML queries work, writing them by hand can be quite laborious.  You can instead use CAML generation tools for this.  My favorite is the U2U CAML Query Builder.  You can download the version for SharePoint 2007 - I have not yet experienced any issues when running this against SharePoint 2010.

Hope this helps.
0 Kudos
HeatherHainsworth
Deactivated User
Wow, that explains a lot and is very helpful! thanks so much for explaining that I had been wondering why the one field in the sample didn't match the list.

Ok, my last question about list queries is there any way to change the symbol of the features returned and placed on the map? It returns as a orange stick pin right now, I have tried setting a graphic symbol in the code but the orange stick pin always overrides it, the code below works in a normal ESRI Silverlight application... is there something special you need to do for SharePoint?

                ESRI.ArcGIS.Client.Symbols.PictureMarkerSymbol Arrow = new PictureMarkerSymbol();

                BitmapImage bitmap = new BitmapImage();
                Uri imgURL = new Uri("http://localhost/ArrowYellow.png", UriKind.Absolute);
                bitmap.UriSource = imgURL;

                Arrow.Source = bitmap;
                graphic.Symbol = Arrow;
0 Kudos
RichZwaap
Frequent Contributor
Set the Renderer property on the GraphicsLayer before adding it to the map.  The symbology functionality in the Map Web Part works with the Renderer property, so it checks this when a layer is added to see whether it is null.  If it is, it adds a simple renderer that specifies the default symbol you are seeing.  If the Renderer is specified, this takes precedence over the Symbol property on individual graphics.
0 Kudos
MichalGrinvald
Esri Contributor
Hi

I have another question...
When I query the list I build the caml with the fields names I get as attributes to the graphics in the SharePointListGraphicsLayer.
These are the fields' internal names.
When I get the result I can only see again the internal name of the fields in the returned ListItemCollection.
How do I get the alias/display name of the fields?

Thanks
Michal
0 Kudos
GarrettMoeller
Deactivated User
So, all elements that call the field name should be using only the internal field name?
There is no use for the alias name (other than for display purposes)?

I am able to get the pop-up to show with the drop-down items,
but when you select, nothing happens afterwards -
no query, no error message, no new layer creation...
0 Kudos