Quantcast
Channel: CamlJs: SharePoint JavaScript Caml Builder
Viewing all 199 articles
Browse latest View live

New Post: Field type Choice with the option multiple selections enabled.

$
0
0
Hi, I don't find the right syntax for the field type Choice with the option multiple selections enabled.

Can you help me?

var sVal = ['N1','M1','M2']

var query = new CamlBuilder()
.Where().LookupMultiField("VehicleCategory")
.IncludesSuchItemThat()
.ValueAsText().In(sVal).ToString()
Image

Thank you,
Mauro

New Post: LookupId='FALSE' how to write in CAMLJS ?

$
0
0
In SharePoint Online (2016), it is possible to use in Managed Metadata text values instead of Integer. Note LookupId='FALSE' and Value Type='Text'. How can I implement the following code in camljs?
<Where>
    <In>
        <FieldRef LookupId='FALSE' Name='Tag' />
        <Values>
            <Value Type='Text'>Anything</Value>
            <Value Type='Text'>Something</Value>
        </Values>
    </In>
</Where>

Created Unassigned: CamlJs ToString undefined error [9]

$
0
0
I am running a simple CamlJs, it works from Chrome Console and from the Chrome Extension. However when I try from a Content Editor Webpart (Sharepoint Online / O365) it gives me an error.

```
<script src="/sites/intranet/Style%20Library/CI/scripts/document_library/camljs.js"></script>
<script type="text/javascript">
var str = new CamlBuilder()
.Where()
.LookupMultiField("Tag")
.IncludesSuchItemThat()
.ValueAsText()
.In(['a', 'b', 'authorization form'])
.ToString();
console.log(str);
</script>
```

This is the error:

```
camljs.js:948 Uncaught TypeError: Cannot read property 'create' of undefined
at Builder.Finalize (camljs.js:948)
at QueryToken.ToString (camljs.js:304)
at doclib.aspx:1123
```

Commented Unassigned: CamlJs ToString undefined error [9]

$
0
0
I am running a simple CamlJs, it works from Chrome Console and from the Chrome Extension. However when I try from a Content Editor Webpart (Sharepoint Online / O365) it gives me an error.

```
<script src="/sites/intranet/Style%20Library/CI/scripts/document_library/camljs.js"></script>
<script type="text/javascript">
var str = new CamlBuilder()
.Where()
.LookupMultiField("Tag")
.IncludesSuchItemThat()
.ValueAsText()
.In(['a', 'b', 'authorization form'])
.ToString();
console.log(str);
</script>
```

This is the error:

```
camljs.js:948 Uncaught TypeError: Cannot read property 'create' of undefined
at Builder.Finalize (camljs.js:948)
at QueryToken.ToString (camljs.js:304)
at doclib.aspx:1123
```
Comments: Hi, The issue most likely happens because SP.StringBuilder that camljs uses has not yet loaded by the time when your inline script executes. You can try the following in order to fix it: * wrap your code into SP.SOD.executeFunc. if I remember correctly, you need to wait for init.js * wrap your code into _spBodyOnLoadFunctions.push(function() { ... }) or $(document).ready * check if you're using the latest version of camljs library - starting from 2.5 CamlJs should be able to run without dependencies

New Post: Documentation

$
0
0
Hello. I have noticed that there is no documentation available whatsoever, and it is really time consuming figuring out how to do the simplests queries.

I know the documentation section says that it's included in "intellisense", but I have no access to "intellisense" technology, since all I can use is SharePoint Designer.

So, where can one such as myself get much needed info about the project? Furthermore, if help is needed in building it, how can I be of assistance?

Thanks in advance!

Created Unassigned: CamlBuilder.FromXml().ModifyWhere() not working correctly [10]

$
0
0
example:
var q = CamlBuilder.FromXml('<View><Query><Where><IsNotNull><FieldRef Name="Title" /></IsNotNull></Where><OrderBy><FieldRef Name="Title" /></OrderBy></Query><RowLimit Paged="TRUE">30</RowLimit><ViewFields><FieldRef Name="ID" /><FieldRef Name="LinkTitle" /></ViewFields></View>');
var result = q.ModifyWhere().AppendAnd().TextField("Title").Contains("test").ToString();

result :

<View>
__<Query>__
<OrderBy>
<FieldRef Name="Title" />
</OrderBy>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="ID" />
<FieldRef Name="LinkTitle" />
</ViewFields>
<Where>
<And>
<IsNotNull>
<FieldRef Name="Title" />
</IsNotNull>
<Contains>
<FieldRef Name="Title" />
<Value Type="Text">test</Value>
</Contains>
</And>
</Where>
__</Query>__
</View>

Instead :
<View>
__<Query>__
<OrderBy>
<FieldRef Name="Title" />
</OrderBy>
<Where>
<And>
<IsNotNull>
<FieldRef Name="Title" />
</IsNotNull>
<Contains>
<FieldRef Name="Title" />
<Value Type="Text">test</Value>
</Contains>
</And>
</Where>
__</Query>__
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="ID" />
<FieldRef Name="LinkTitle" />
</ViewFields>
</View>

Commented Unassigned: CamlBuilder.FromXml().ModifyWhere() not working correctly [10]

$
0
0
example:
var q = CamlBuilder.FromXml('<View><Query><Where><IsNotNull><FieldRef Name="Title" /></IsNotNull></Where><OrderBy><FieldRef Name="Title" /></OrderBy></Query><RowLimit Paged="TRUE">30</RowLimit><ViewFields><FieldRef Name="ID" /><FieldRef Name="LinkTitle" /></ViewFields></View>');
var result = q.ModifyWhere().AppendAnd().TextField("Title").Contains("test").ToString();

result :

<View>
__<Query>__
<OrderBy>
<FieldRef Name="Title" />
</OrderBy>
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="ID" />
<FieldRef Name="LinkTitle" />
</ViewFields>
<Where>
<And>
<IsNotNull>
<FieldRef Name="Title" />
</IsNotNull>
<Contains>
<FieldRef Name="Title" />
<Value Type="Text">test</Value>
</Contains>
</And>
</Where>
__</Query>__
</View>

Instead :
<View>
__<Query>__
<OrderBy>
<FieldRef Name="Title" />
</OrderBy>
<Where>
<And>
<IsNotNull>
<FieldRef Name="Title" />
</IsNotNull>
<Contains>
<FieldRef Name="Title" />
<Value Type="Text">test</Value>
</Contains>
</And>
</Where>
__</Query>__
<RowLimit Paged="TRUE">30</RowLimit>
<ViewFields>
<FieldRef Name="ID" />
<FieldRef Name="LinkTitle" />
</ViewFields>
</View>

Comments: PS: need to check if Where is empty , so not needed to append And or Or

New Post: Order By modification

$
0
0
Hi,
It would be great to implement something for modifying existing OrderBy string in the CamlBuilder.FromXml.
Thank you

Commented Unassigned: CamlJs ToString undefined error [9]

$
0
0
I am running a simple CamlJs, it works from Chrome Console and from the Chrome Extension. However when I try from a Content Editor Webpart (Sharepoint Online / O365) it gives me an error.

```
<script src="/sites/intranet/Style%20Library/CI/scripts/document_library/camljs.js"></script>
<script type="text/javascript">
var str = new CamlBuilder()
.Where()
.LookupMultiField("Tag")
.IncludesSuchItemThat()
.ValueAsText()
.In(['a', 'b', 'authorization form'])
.ToString();
console.log(str);
</script>
```

This is the error:

```
camljs.js:948 Uncaught TypeError: Cannot read property 'create' of undefined
at Builder.Finalize (camljs.js:948)
at QueryToken.ToString (camljs.js:304)
at doclib.aspx:1123
```
Comments: Hi, I am using Release 2.7 and still getting the error, this is how I am wrapping the call to camljs ``` jQuery(document).ready(function() { SP.SOD.executeOrDelayUntilScriptLoaded(call_camljsfunction,'sp.js'); }); ``` Still getting errors in IE11 but working in Chrome.

New Post: How to write a join of join

$
0
0
Are there a way to get a lookup from lookup?

I have a people list with a lookup to departments list and inside departments i have a lookup to the list localisations.

How to get the three informations from the first list people?


thansk

Created Unassigned: Operation is not valid due to the current state of the object [11]

$
0
0
Hi I'm trying to write this simple caml query (just getting all items)
'<View><Query><OrderBy><FieldRef Name="ID" Ascending="TRUE"></FieldRef></OrderBy></Query></View>'

Made it that way : new CamlBuilder().View().Query().OrderBy("ID").ToString(); but i get an annoying error
Uncaught Error: Sys.InvalidOperationException: Operation is not valid due to the current state of the object.

Am I doing something wrong ?

Thanks

Updated Wiki: Home

$
0
0

Project moved to https://github.com/andrei-markeev/camljs

 

Project Description

Simplifies creation of SharePoint CAML queries for client-side scripts. Can be used with either SharePoint Client Object Model or SPServices.

Nuget:

PM> Install-Package CamlJs

TypeScript definitions:

PM> Install-Package camljs.TypeScript.DefinitelyTyped

What's new in Release 2.7

    1. New type of field - ModStatField
    2. Fixed bug with boolean field EqualTo operation

What's new in Release 2.6

      1. Now it is possible to use CamlJs for modifying existing CAML queries:
        • FromXml method will create a CamlBuilder object from existing CAML string
        • ReplaceWhere method allows to replace <Where> clause with one generated by CamlJs
        • ModifyWhere().AppendAnd() will add new conditions to existing query using "And" operator
        • ModifyWhere().AppendOr() will add new conditions to existing query using "Or" operator

        Example:

      2. Added Query().GroupBy(...) and Query().OrderBy(...)

 

Check out CamlJs-Console!

I created a Chrome extension for instant testing CamlJs queries against real lists,CamlJs-Console. The extension is open source and free. Available via Web Store or alternatively you can install it from the source code (see docs on GitHub).

What's new in Release 2.5

View element

View element is added! Scope attribute, ViewFields, Joins and ProjectedFields are supported. Joining lists via CamlJs is very easy. Here's the example:

var query = new CamlBuilder()
    .View(["Title","Country","Population"])
    .LeftJoin("Country","Country").Select("People","Population")
    .Query()
    .Where()
    .NumberField("Population").LessThan(10)
    .ToString();

Intellisense helps understanding the parameters:

And one more screenshot of intellisense:

So as you see, really simple: Join listName as Alias, Select Field as Alias, Select Field as Alias, etc. Then, use fields aliases in the main query. That's it! Corresponding Joins and ProjectedFields elements are generated automatically.

So the resulting generated CAML query will be the following:

<View><ViewFields><FieldRefName="Title"/><FieldRefName="Country"/><FieldRefName="Population"/></ViewFields><Joins><JoinType="LEFT"ListAlias="Country"><Eq><FieldRefName="Country"RefType="ID"/><FieldRefName="ID"List="Country"/></Eq></Join></Joins><ProjectedFields><FieldShowField="People"Type="Lookup"Name="Population"List="Country"/></ProjectedFields><Query><Where><Lt><FieldRefName="Population"/><ValueType="Number">10</Value></Lt></Where></Query></View>

No dependencies

CamlJs was dependent on some SharePoint components (Sys.StringBuilder and SP.XmlWriter). But many people had problems with that, so now CamlJs doesn't have any dependencies.

This enables opportunity to run CamlJs standalone, e.g. in SharePoint Apps without including heavy SP javascripts; and also to use CamlJs in SharePoint 2007. Though please remember that some CAML Query elements (e.g. <In>) were added only in SP2010.

Migration from 2.0 to 2.5

Please remember that some fields were marked as DEPRECATED in Release 2.0. In version 2.5, previously marked LookupIdField is removed. In next versions, the process will be continued.

If you're still using LookupIdField, replace it like this:

Old code example:

.LookupIdField("MyLookup").EqualTo(5)

Replace with:

.LookupField("MyLookup").Id().EqualTo(5)

ToCamlQuery()

Added ToCamlQuery method, that can be to finalize the query. But instead of string, ToCamlQuery will produce a ready-to-use SP.CamlQuery object, which you can then pass into list.getItems method.

Example project

Example project is now a SharePoint-hosted app with a simple interactive demo:

UserMultiField and LookupMultiField

UserMultiField and LookupMultiField weren't much tested in previous release, but now in 2.5 they are tested through and significantly improved. Now instead old EqualTo, NotEqualTo, Includes and NotIncludes operations (these are marked DEPRECATED), single operation "IncludesSuchItemThat" added.

 

So now it is possible to check condition against Id or against a value, cast to a specified type (same as with single-value lookups):

Internally, CAML Query is identical for MultiLookup and single-value Lookup columns, but hopefully this minor syntax improvement will help people better understand how SharePoint interprets this query.

---------------- end of release 2.5 news -------------

Basics

To start with, let's assume we need to fetch all Google-related letters from some mailbox list, using SharePoint Client Object Model. To generate the corresponding query using Caml Builder, you could use following javascript code:

var camlBuilder = new CamlBuilder();

    var caml = camlBuilder.Where()
        .TextField("Email").EqualTo("support@google.com")
        .Or()
        .TextField("Email").EqualTo("plus@google.com")
        .Or()
        .TextField("Title").BeginsWith("[Google]")
        .Or()
        .TextField("Content").Contains("Google")
        .ToString();

This will generate expected CAML:

<Where><Or><Eq><FieldRefName="Email"/><ValueType="Text">support@google.com</Value></Eq><Or><Eq><FieldRefName="Email"/><ValueType="Text">plus@google.com</Value></Eq><Or><BeginsWith><FieldRefName="Title"/><ValueType="Text">[Google]</Value></BeginsWith><Contains><FieldRefName="Content"/><ValueType="Text">Google</Value></Contains></Or></Or></Or></Where>

, which then could be used in SP.CamlQuery.

Very good so far, and have a look at some other examples!

First of all, cannot resist to mention, that CamlBuilder covers almost all Query elements, described at MSDN.

For example, seldom used Membership element:

var caml = camlBuilder.Where()
        .UserField("AssignedTo").EqualToCurrentUser()
        .Or()
        .UserField("AssignedTo").IsInCurrentUserGroups()
        .GroupBy("ProductTitle")
        .OrderBy("Priority").ThenBy("Title")
        .ToString();

This code will generate following CAML:

<Where><Or><Eq><FieldRefName="AssignedTo"/><ValueType="Integer"><UserID/></Value></Eq><MembershipType="CurrentUserGroups"><FieldRefName="AssignedTo"/></Membership></Or></Where><GroupBy><FieldRefName="ProductTitle"/></GroupBy><OrderBy><FieldRefName="Priority"/><FieldRefName="Title"/></OrderBy>

The last example at this section:

    caml = camlBuilder.Where()
        .LookupField("Category").Id().In([2, 3, 10])
        .And()
        .DateField("ExpirationDate").LessThanOrEqualTo(CamlBuilder.CamlValues.Now)
        .OrderByDesc("ExpirationDate")
        .ToString()

As you see, the code is pretty clean and readable. The resulting CAML is much more awkward, especially if you imagine it in javascript strings dress, without indentation and highlighting...

<Where><And><In><FieldRefName="Category"LookupId="True"/><Values><ValueType="Integer">2</Value><ValueType="Integer">3</Value><ValueType="Integer">10</Value></Values></In><Leq><FieldRefName="ExpirationDate"/><ValueType="Date"><Now/></Value></Leq></And></Where><OrderBy><FieldRefName="ExpirationDate"Ascending="False"/></OrderBy>

Usability

CamlBuilder is not only readable and simple, but it also provides rich intellisense and inline documentation:

This will help to catch a bunch of errors when writing code, and surely prevent from typos and misunderstandings, so the benefits could be really magnificient!

Intellisense and inline docs are even better if you use CamlJs with TypeScript.

Contribute

Any contributions are highly appreciated!

Source code checked in, #cd451a9c89b65ecf1704ad11acfa6af4a9473408

Source code checked in, #1b0973dc5fe3b5360f96e7f56d551357a8bacc73

Source code checked in, #81dbab1c4543f29ea1ec436638da9612c2ad3eee

$
0
0
Merge branch 'master' of https://git01.codeplex.com/camljs

New Post: how to compose query to list having more than 5000 items

$
0
0
how to compose query to list having more than 5000 items?

Created Unassigned: Error loading camljs package into spfx solution [12]

$
0
0

Im trying to load camljs.js ina client webpart using the SharePoint Framework Experience.I am getting the following error

*ERROR MESSAGE:
***Failed to load component "df52bb7c-0165-4fa5-adfa-1e1c91740246" (Test2WebPart).
Original error: ***Failed to load path dependency "df52bb7c-0165-4fa5-adfa-1e1c91740246" from component "Test2WebPart" (Error loading https://component-id.invalid/df52bb7c-0165-4fa5-adfa-1e1c91740246_0.0.1/camljs
https://component-id.invalid/df52bb7c-0165-4fa5-adfa-1e1c91740246_0.0.1/camljs did not call System.register or AMD define. If loading a global module configure the global name via the meta exports property for script injection support.).

However, the network tab in Chrome developer tools shows that camljs.js is loading


The solution is using the React template. After scaffolding has been completed, I am installing camljs as follows

npm install --save camljs
npm install --save @types/camljs

I have also excluded camljs from being bundled in the config file (externals) and have set a globalname:

"camljs":{
"path" : "node_modules/camljs/camljs.js",
"globalName": "camljs"
}
}

in the solutions. tsx file I am importing camljs in the following way:

import camljs from 'camljs'

but the module cannot be found using intellisense in Visual Code

Any thoughts as to how to fix this?

Commented Unassigned: Error loading camljs package into spfx solution [12]

$
0
0

Im trying to load camljs.js ina client webpart using the SharePoint Framework Experience.I am getting the following error

*ERROR MESSAGE:
***Failed to load component "df52bb7c-0165-4fa5-adfa-1e1c91740246" (Test2WebPart).
Original error: ***Failed to load path dependency "df52bb7c-0165-4fa5-adfa-1e1c91740246" from component "Test2WebPart" (Error loading https://component-id.invalid/df52bb7c-0165-4fa5-adfa-1e1c91740246_0.0.1/camljs
https://component-id.invalid/df52bb7c-0165-4fa5-adfa-1e1c91740246_0.0.1/camljs did not call System.register or AMD define. If loading a global module configure the global name via the meta exports property for script injection support.).

However, the network tab in Chrome developer tools shows that camljs.js is loading


The solution is using the React template. After scaffolding has been completed, I am installing camljs as follows

npm install --save camljs
npm install --save @types/camljs

I have also excluded camljs from being bundled in the config file (externals) and have set a globalname:

"camljs":{
"path" : "node_modules/camljs/camljs.js",
"globalName": "camljs"
}
}

in the solutions. tsx file I am importing camljs in the following way:

import camljs from 'camljs'

but the module cannot be found using intellisense in Visual Code

Any thoughts as to how to fix this?

Comments: Hi, Sorry for a delay with response. I think the `"globalName": "camljs"` piece is incorrect. Library name is indeed "camljs", but the global object is "CamlBuilder". Can you try changing it? Best, Andrei

New Post: Looking between 2 dates

$
0
0
Hello, I come today for some help, I'm trying to get result between two dates

So far the only thing I can think of is
        _var variable1 = sessionStorage.getItem('variable1');
        var variable2 = sessionStorage.getItem('variable2');
        var query = camlBuilder.Where().DateField("Created").EqualTo(variable1).And().DateField("Created").LessThanOrEqualTo(variable2).ToCamlQuery();_
This caml is new to me so I would apreciate it so much any help.

In SQL it would be something like " WHERE date BETWEEN 'variable1' AND 'variable2'"

Thank you in advance
Viewing all 199 articles
Browse latest View live