0

I have a standard CAML query (see below) that I would like to perform in SharePoint designer. How do I take this CAML query and retrieve it's data. Also where do I declare the list to run it against

<Where>
<Eq>
<FieldRef Name="Title" />
<Value Type="Text">Test</Value>
</Eq>
</Where>
<OrderBy>
<FieldRef Name="Created" Ascending="False" />
</OrderBy>
3

If you requirement is to simply check the results of the query then you can use a tool like U2U Caml Query Builder. This tool allows you to create and execute the queries on a given list without the need to write any code. Moreover, it also generates a sample managed client object model code and server object model code which can be used in applications like a console application.

However, if your requirement is to use the CAML Query in a SharePoint designer, then only having a query is not sufficient. You may need to write JavaScript client object model code inside the designer. The code will get the list and execute query against it and display the result. Example code:

function retrieveListItems() {
    var clientContext = new SP.ClientContext.get_current();
    var oList = clientContext.get_web().get_lists().getByTitle('YourListName');

    var camlQuery = new SP.CamlQuery();
    camlQuery.set_viewXml(
        '<View><Query><Where><Geq><FieldRef Name=\'ID\'/>' + 
        '<Value Type=\'Number\'>1</Value></Geq></Where></Query>' + 
        '<RowLimit>10</RowLimit></View>'
    );
    this.collListItem = oList.getItems(camlQuery);

    clientContext.load(collListItem);
    clientContext.executeQueryAsync(
        Function.createDelegate(this, this.onQuerySucceeded), 
        Function.createDelegate(this, this.onQueryFailed)
    ); 
}

function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();
        listItemInfo += '\nID: ' + oListItem.get_id() + 
            '\nTitle: ' + oListItem.get_item('Title') + 
            '\nBody: ' + oListItem.get_item('Body');
    }

    alert(listItemInfo.toString());
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + 
        '\n' + args.get_stackTrace());
}

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveListItems);
| improve this answer | |
1

You can create a page and using sharepoint designer and add the list view web part/ xsltlistview webpart you can use the CAML query and fetch the list data. Following is example of xsltlistview webpart to show the data based on CAML query.

<WebPartPages:XsltListViewWebPart runat="server" IsIncluded="True"
                        GhostedXslLink="main.xsl" FrameType="None" NoDefaultStyle="TRUE" ViewFlag="8"
                        Title="My Request" PageType="PAGE_NORMALVIEW" Default="FALSE"
                        __markuptype="vsattributemarkup" PartOrder="2"
                        __WebPartId="{91EB7C29-AED3-44A3-B01E-54F1893A48DC}"
                        ID="g_91eb7c29_aed3_44a3_b01e_54f1893a48dc"
                        ViewContentTypeId="0x" __designer:customxsl="fldtypes_Ratings.xsl" ListDisplayName="ProductList">
                        <XmlDefinition>
                <View Name="{78E6EC9D-1910-4B6D-9030-E88975FAA2EC}" 
                    MobileView="TRUE" 
                    Type="HTML" DisplayName="All Items" 
                     Level="1" BaseViewID="1" ContentTypeID="0x" ImageUrl="/_layouts/15/images/generic.png?rev=23">
                    <Query>
                        <Where>
                            <Eq>
                                <FieldRef Name="CreatedBy"/>
                                <Value Type="Integer">
                                    <UserID Type="Integer"/>
                                </Value>
                            </Eq>
                        </Where>
                    </Query>
                    <ViewFields>
                        <FieldRef Name="LinkTitle"/>
                        <FieldRef Name="Product"/>
                        <FieldRef Name="Price"/>
                        <FieldRef Name="Quantity"/>
                        <FieldRef Name="Status"/>
                        <FieldRef Name="Supervisor"/>
                    </ViewFields>
                    <RowLimit Paged="TRUE">30</RowLimit>
                    <Aggregations Value="Off"/>
                    <JSLink>clienttemplates.js</JSLink>
                    <XslLink Default="TRUE">main.xsl</XslLink>
                    <Toolbar Type="None"/>
                </View>
                        </XmlDefinition>
                        <ParameterBindings>
                <ParameterBinding Name="dvt_sortdir" Location="Postback;Connection"/>
                <ParameterBinding Name="dvt_sortfield" Location="Postback;Connection"/>
                <ParameterBinding Name="dvt_startposition" Location="Postback" DefaultValue=""/>
                <ParameterBinding Name="dvt_firstrow" Location="Postback;Connection"/>
                <ParameterBinding Name="OpenMenuKeyAccessible" Location="Resource(wss,OpenMenuKeyAccessible)" />
                <ParameterBinding Name="open_menu" Location="Resource(wss,open_menu)" />
                <ParameterBinding Name="select_deselect_all" Location="Resource(wss,select_deselect_all)" />
                <ParameterBinding Name="idPresEnabled" Location="Resource(wss,idPresEnabled)" />
                <ParameterBinding Name="NoAnnouncements" Location="Resource(wss,noXinviewofY_LIST)" />
                <ParameterBinding Name="NoAnnouncementsHowTo" Location="Resource(core,noXinviewofY_DEFAULT)" />
                <ParameterBinding Name="AddNewAnnouncement" Location="Resource(wss,addnewitem)" />
                <ParameterBinding Name="MoreAnnouncements" Location="Resource(wss,moreItemsParen)" />
                        </ParameterBindings>
                    </WebPartPages:XsltListViewWebPart>
                </ZoneTemplate>
            </WebPartPages:WebPartZone>
| improve this answer | |
1

I think SharePoint Designer is not meant for this .

First of all you need a List in your Sharepoint site for the CAML query to get the items from it.

How to create a List:http://www.sharepoint-journey.com/create-list-in-sharepoint-2013.html

Now there are two conditions:

  1. If you need all items in the List: No need of CAML then.

    SPList list = web.Lists["MyList"];

    SPListItemCollection items = list.Items;

  2. If you need specific items from the list:Use CAML query There are certain tools which you can install to test and see the results of your CAML query

http://sharepoint-works.blogspot.in/2012/05/using-caml-query-builder-for-sharepoint.html

https://spcamlqueryhelper.codeplex.com/

| improve this answer | |
0

I don't think that it's possible with SharePoint Designer. I highly recommend you using this tool (http://splistitemeditor.codeplex.com/) or this tool (https://spcamlqueryhelper.codeplex.com/). These tools will let you try your CAML queries and display your results in Gridviews.

| improve this answer | |
0

In addition to utilizing the web parts, discussed above, you can also leverage the SharePoint Client Object Model/SPServices. There is a codeplex solution, CamlJs, which provides some ease to the process, and helps you build JS that will complete your desired solution.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.