Showing posts with label JSOM. Show all posts
Showing posts with label JSOM. Show all posts

Tuesday, April 15, 2014

Large Lists, BCS, Excel REST, JSOM, CSVs and Office 365 OH MY!

An interesting challenge came across my desk last week:
"How can we import a CSV into Office 365 and use that data to tag other items within the site.  The CSV currently has ~7600 rows and is expected to grow.  We'll also need to re-import the CSV on an ad-hoc basis when the data changes."
The last part of that was the real issue.  There was simply no easy way of doing that.  After trying a few things, falling flat on my face, I'm prepared to share my exploration into the different options.

Each have their own merits and pitfalls.  This post will examine each and try to shed some light on the pitfalls I've found using each of these.

BCS

Wiring up an ECT on Office 365 can be a little finicky. I initially had some issues due to 2 BDC Models that were created for the same ECT.  After calling in the eagle-eyed BCS guru, +Fabian Williams, I got squared away.

Immediately after that, I could tell that I was not going to be able to use BCS for what I needed.  In Office 365, there is a hard limit of 2000 items that can be retrieved.  Ironically, the error message that is displayed is not a supported cmdlet for Office 365.

Office 365 BCS Throttle Error
Adding insult to injury, I decided to run a simple test using JSOM.  I wanted to clarify if JSOM would provide me PagingInfo with a BCS List.  Using the code below, you'd expect line 68 to produce a value instead of nothing at all.


Since BCS will not work and due to the limitation of the API, I had to look for another solution.

Custom List

Using a custom list initially worked great.  I'm able to use JSOM, query the list for 5000 items per trip to the server, AND get PagingInfo.  Using the code below works great for this scenario.


Importing the Excel into Office 365 is relatively straightforward and will work for most needs.  The file I used had about ~7600 rows of data.  After importing the file, I noticed the Server Resource Quota was tapped.

Office 365 Server Utilization
So using this approach has 2 problems.  I will not be able to do a mass import again of my data (the list already exists) and the Server Resource Quota points are exhausted.

Excel REST

This seemed like a cool way of getting around the limitations above, so I dove in to find out if this will work for my needs.  After all, I'm allowed to have a *lot* of rows in Excel and I'll be able to easily update the file, since it's in a document library.  Using the code below, I ran into a showstopper though.


There is a hard limit in the API set at 500 rows.  That would be painfully slow to get all of the items or even worse; a user may try to use the form control while this is still querying for data.

Excel REST API - 500 row limit
So that leaves us with our raw data that was exported from SQL and given to us to use.

CSV

Updating the CSV will be easy, since it will be stored in a document library.  Now all we need to be able to do is make sense of it.  Using the code below, I'm able to parse the CSV and create an array of objects that I need to pass off to another library.  Also note the use of localStorage.  This is a nice way to cache the data and prevent the retrieval/processing of the data client-side on every page load.  If the CSV is updated, simply clear the browser cache and you'll get the latest and greatest.

Conclusion

All approaches have their merits and pitfalls... BCS and PagingInfo, I'm looking at you!  If the ad-hoc mass-import wasn't needed, then using list driven data would have been my choice.  If I used that approach, I would have still used localStorage though.  It makes sense to cache the processed data since it'll not change very much.  Since my solution works client-side, I'll have to take into consideration the amount of time this takes to render.  I'm getting good performance out of the CSV approach, so I'm going to stick with it for the time being.

Thursday, March 21, 2013

RowLimit and CAML Query

I keep forgetting this, so here's a little note for me:

"<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='Author'/><Value Type='Integer'><UserID/></Value></Eq></Where><OrderBy><FieldRef Name='Created' Ascending='False' /></OrderBy></Query><RowLimit>1</RowLimit></View>"

There you have it... A valid usage of RowLimit within a CAML Query.

Tuesday, February 12, 2013

Setting field values using CSOM client side - Another look

Last night after hitting publish, I enjoyed a long ride home on the metro...  I was able to catch up on some of my reading.  It's a relaxing part of my day and sometimes exciting because I get to grind away on concepts that I'm working on.  Last night did not disappoint.

spUtils - setColumnVal


As I said in the previous post, I've already tackled this problem, however, I didn't really like the implementation.  So, here's my bright idea... Toggle the library back to use setColumnVal and see what the XML looks like under the hood.  Doing just that, here's what's produced( I've snipped this for brevity ):


<Method Name="SetFieldValue" Id="26" ObjectPathId="21">
  <Parameters>
<Parameter Type="String">AssignedTo</Parameter>
<Parameter Type="Array">
<Object TypeId="{c956ab54-16bd-4c18-89d2-996f57282a6f}">
<Property Name="LookupValue" Type="String">DEV\Administrator</Property>
<Property Name="LookupId" Type="Number">-1</Property>
</Object>
<Object TypeId="{c956ab54-16bd-4c18-89d2-996f57282a6f}">
<Property Name="LookupValue" Type="String">DEV\spUser</Property>
<Property Name="LookupId" Type="Number">-1</Property>
</Object>
</Parameter>
</Method>


So based on that, it's easy to see the people picker XML has to be an array of objects.  Let's give that a shot now using this code mixed with parseAndSetFieldValue.

spUtils - parseAndSetFieldValue revisited


spUtils.updateListItems({
listName: "spUtils",
updates : {
1 : {
"Title" : spUtils.isoDate(),
"AssignedTo" : [
{
LookupValue: "DEV\\Administrator",
LookupId: -1
},
{
LookupValue: "DEV\\spUser",
LookupId: -1
}
]
}
},
success: function( data, ctx ) {
debugger;
}
});

Using the code above produces this XML ( snipped as well for brevity ):

<Method Name="ParseAndSetFieldValue" Id="44" ObjectPathId="21">
  <Parameters>
<Parameter Type="String">AssignedTo</Parameter>
<Parameter Type="Array">
<Object Type="Dictionary">
<Property Name="LookupValue" Type="String">DEV\Administrator</Property>
<Property Name="LookupId" Type="Number">-1</Property>
</Object>
<Object Type="Dictionary">
<Property Name="LookupValue" Type="String">DEV\spUser</Property>
<Property Name="LookupId" Type="Number">-1</Property>
</Object>
</Parameter>
  </Parameters>
</Method>


It's remarkably close to the XML that actually works.  The only thing that is different is the Object Type.  Sadly, this is all that it takes for this to FAIL.  Yep, that's right...  Trying to be smarter than the average bear, let's give it another shake.  This time, I'm going to take some code out of the setColumnVal method and drop it into an array.  Take a look at this:

spUtils.updateListItems({
listName: "spUtils",
updates : {
1 : {
"Title" : spUtils.isoDate(),
"AssignedTo" : [  SP.FieldUserValue.fromUser("DEV\\Administrator"),
  SP.FieldUserValue.fromUser("DEV\\spUser")
]
}
},
success: function( data, ctx ) {
debugger;
}
});


This in turn produces XML that *should* work!

<Method Name="ParseAndSetFieldValue" Id="44" ObjectPathId="21">
  <Parameters>
  <Parameter Type="String">AssignedTo</Parameter>
  <Parameter Type="Array">
  <Object TypeId="{c956ab54-16bd-4c18-89d2-996f57282a6f}">
  <Property Name="LookupValue" Type="String">DEV\Administrator</Property>
<Property Name="LookupId" Type="Number">-1</Property>
  </Object>
  <Object TypeId="{c956ab54-16bd-4c18-89d2-996f57282a6f}">
<Property Name="LookupValue" Type="String">DEV\spUser</Property>
<Property Name="LookupId" Type="Number">-1</Property>
  </Object>
  </Parameter>
  </Parameters>
</Method>


The only difference this time is the Method Name attribute.  Sadly, even this FAILS! I was going to continue with using numbers, but with this being a show stopper, I'm convinced I've researched this thoroughly enough.  This may be different in SP2013, it simply doesn't work in SP2010, therefore unreliable.

What's next?


Since I need the context of the list item to set its values when using the .update() method, it's not feasible to change what I have currently.  To set lookups and people picker values in CSOM, you have to use the code I've already written.  Guess it's time I start documenting the API, eh?

Monday, February 11, 2013

Setting field values using CSOM client side

A rather long time ago, I wrote some code to handle creation and updates of items when using Client Object Model.  This bit of code works very well however it's always made me feel like the API I've built had warts. Take a look for yourself.  This method allows for anyone to modify Lookup values as well as People Picker values.  Using this, you can also set multiple values without a problem.  That's the good...  The ugly is for this to work, it's necessary to tell spUtils the column type.  This ended up looking something like this:


/***************************************
Test 26 ~ updateListItems - Updates item's lookup and people picker column
***************************************/
spUtils.updateListItems({
listName : "Project Tasks",
updates : {
111 : { // the key is the item ID
"RelatedProject{L}" : spUtils.isoDate(),
"AssignedTo{P}" : 1,

     "Title" : "Hello, World!"
}
},
success : function() { debugger; }
});


// End codez


Notice the appended characters that represent the column type. This is fine but at the end of the day not very user friendly...  I've always thought I could do better.  This blog post will explore what I've found inside the SP Namespace and what my options are to fix it.

SP.Debug.js to the Rescue?

When looking at a listItem object in my debugger, I found a rather coy method.  It literally screamed at me: "Put me in the game coach!"... Totally looked over this one when building CRUD into spUtils.  With that said, I've made the change under the hood to use this method instead.

parseAndSetFieldValue: function(fieldName, value_) {ULS5Vl:;
        this.get_fieldValues()[fieldName] = value_;
        var $v_0 = new SP.ClientActionInvokeMethod(this, 'ParseAndSetFieldValue', [ fieldName, value_ ]);
        if ((this.get_context())) {
            this.get_context().addQuery($v_0);
        }
},


For posterity, here's the raw bits of the function call. Nothing too exciting here since it's really a wrapper for the ClientActionInvokeMethod. Come to think of it, what isn't a wrapper for ClientActionInvokeMethod inside SP.js?

Is parseAndSetFieldValue really up to snuff?

For me it was truly a magical moment coming across this method.  As of matter of fact, I made a note of it when I did to come back someday and put the spotlight directly on it...  As of a result, here are my findings in raw format ( I've highlighted the important pieces of XML ):


<!-- Test 1
[ "AssignedTo", ["DEV\\Administrator", "DEV\\spuser"] ]
XML sent to Server using Array of strings
-->

<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="14.0.0.0" LibraryVersion="14.0.4762.1000" ApplicationName="Javascript Library">
<Actions>
<ObjectPath Id="1" ObjectPathId="0" />
<ObjectPath Id="3" ObjectPathId="2" />
<ObjectPath Id="5" ObjectPathId="4" />
<ObjectPath Id="7" ObjectPathId="6" />
<ObjectIdentityQuery Id="8" ObjectPathId="6" />
<ObjectPath Id="10" ObjectPathId="9" />
<Method Name="ParseAndSetFieldValue" Id="11" ObjectPathId="9">
<Parameters>
<Parameter Type="String">Title</Parameter>
<Parameter Type="String">2013-02-11T20:10:13Z</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="12" ObjectPathId="9" />
<Query Id="13" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
</Properties>
</Query>
</Query>
<Method Name="ParseAndSetFieldValue" Id="14" ObjectPathId="9">
<Parameters>
<Parameter Type="String">AssignedTo</Parameter>
<Parameter Type="Array">
<Object Type="String">DEV\Administrator</Object>
<Object Type="String">DEV\spuser</Object>
</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="15" ObjectPathId="9" />
<Query Id="16" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
<Property Name="AssignedTo" ScalarProperty="true" />
</Properties>
</Query>
</Query>
<Method Name="ParseAndSetFieldValue" Id="17" ObjectPathId="9">
<Parameters>
<Parameter Type="String">RelatedProject</Parameter>
<Parameter Type="Number">2</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="18" ObjectPathId="9" />
<Query Id="19" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
<Property Name="AssignedTo" ScalarProperty="true" />
<Property Name="RelatedProject" ScalarProperty="true" />
</Properties>
</Query>
</Query>
</Actions>
<ObjectPaths>
<StaticProperty Id="0" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
<Property Id="2" ParentId="0" Name="Web" />
<Property Id="4" ParentId="2" Name="Lists" />
<Method Id="6" ParentId="4" Name="GetByTitle">
<Parameters>
<Parameter Type="String">Project Tasks</Parameter>
</Parameters>
</Method>
<Method Id="9" ParentId="6" Name="GetItemById">
<Parameters>
<Parameter Type="Number">42</Parameter>
</Parameters>
</Method>
</ObjectPaths>
</Request>

Result:
Request failed. Invalid look-up value. A look-up field contains invalid data. Please check the value and try again. 


<!-- Test 2 
[ "AssignedTo", "DEV\\Administrator, DEV\\spuser" ]
XML sent to Server using comma separated values inside a string.
-->

<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="14.0.0.0" LibraryVersion="14.0.4762.1000" ApplicationName="Javascript Library">
<Actions>
<ObjectPath Id="1" ObjectPathId="0" />
<ObjectPath Id="3" ObjectPathId="2" />
<ObjectPath Id="5" ObjectPathId="4" />
<ObjectPath Id="7" ObjectPathId="6" />
<ObjectIdentityQuery Id="8" ObjectPathId="6" />
<ObjectPath Id="10" ObjectPathId="9" />
<Method Name="ParseAndSetFieldValue" Id="11" ObjectPathId="9">
<Parameters>
<Parameter Type="String">Title</Parameter>
<Parameter Type="String">2013-02-11T20:16:15Z</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="12" ObjectPathId="9" />
<Query Id="13" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
</Properties>
</Query>
</Query>
<Method Name="ParseAndSetFieldValue" Id="14" ObjectPathId="9">
<Parameters>
<Parameter Type="String">AssignedTo</Parameter>
<Parameter Type="String">DEV\Administrator; DEV\spuser</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="15" ObjectPathId="9" />
<Query Id="16" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
<Property Name="AssignedTo" ScalarProperty="true" />
</Properties>
</Query>
</Query>
</Actions>
<ObjectPaths>
<StaticProperty Id="0" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
<Property Id="2" ParentId="0" Name="Web" />
<Property Id="4" ParentId="2" Name="Lists" />
<Method Id="6" ParentId="4" Name="GetByTitle">
<Parameters>
<Parameter Type="String">Project Tasks</Parameter>
</Parameters>
</Method>
<Method Id="9" ParentId="6" Name="GetItemById">
<Parameters>
<Parameter Type="Number">42</Parameter>
</Parameters>
</Method>
</ObjectPaths>
</Request>

Result:
Request failed. Invalid data has been used to update the list item. The field you are trying to update may be read only. 

<!-- Test 3
[ "AssignedTo", "DEV\\Administrator;#DEV\\spuser" ]
XML sent to Server using the old semi-colon bang delimiter.
-->

<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="14.0.0.0" LibraryVersion="14.0.4762.1000" ApplicationName="Javascript Library">
<Actions>
<ObjectPath Id="1" ObjectPathId="0" />
<ObjectPath Id="3" ObjectPathId="2" />
<ObjectPath Id="5" ObjectPathId="4" />
<ObjectPath Id="7" ObjectPathId="6" />
<ObjectIdentityQuery Id="8" ObjectPathId="6" />
<ObjectPath Id="10" ObjectPathId="9" />
<Method Name="ParseAndSetFieldValue" Id="11" ObjectPathId="9">
<Parameters>
<Parameter Type="String">Title</Parameter>
<Parameter Type="String">2013-02-11T20:28:42Z</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="12" ObjectPathId="9" />
<Query Id="13" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
</Properties>
</Query>
</Query>
<Method Name="ParseAndSetFieldValue" Id="14" ObjectPathId="9">
<Parameters>
<Parameter Type="String">AssignedTo</Parameter>
<Parameter Type="String">DEV\Administrator;#DEV\spuser</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="15" ObjectPathId="9" />
<Query Id="16" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
<Property Name="AssignedTo" ScalarProperty="true" />
</Properties>
</Query>
</Query>
</Actions>
<ObjectPaths>
<StaticProperty Id="0" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
<Property Id="2" ParentId="0" Name="Web" />
<Property Id="4" ParentId="2" Name="Lists" />
<Method Id="6" ParentId="4" Name="GetByTitle">
<Parameters>
<Parameter Type="String">Project Tasks</Parameter>
</Parameters>
</Method>
<Method Id="9" ParentId="6" Name="GetItemById">
<Parameters>
<Parameter Type="Number">42</Parameter>
</Parameters>
</Method>
</ObjectPaths>
</Request>

Result:
Request failed. Invalid look-up value.  A look-up field contains invalid data. Please check the value and try again. 

<!-- Test 4 
[ "AssignedTo", "DEV\\Administrator; DEV\\spuser" ]
XML sent to Server using text similar to typing into the control manually.
-->

<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="14.0.0.0" LibraryVersion="14.0.4762.1000" ApplicationName="Javascript Library">
<Actions>
<ObjectPath Id="1" ObjectPathId="0" />
<ObjectPath Id="3" ObjectPathId="2" />
<ObjectPath Id="5" ObjectPathId="4" />
<ObjectPath Id="7" ObjectPathId="6" />
<ObjectIdentityQuery Id="8" ObjectPathId="6" />
<ObjectPath Id="10" ObjectPathId="9" />
<Method Name="ParseAndSetFieldValue" Id="11" ObjectPathId="9">
<Parameters>
<Parameter Type="String">Title</Parameter>
<Parameter Type="String">2013-02-11T20:32:14Z</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="12" ObjectPathId="9" />
<Query Id="13" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
</Properties>
</Query>
</Query>
<Method Name="ParseAndSetFieldValue" Id="14" ObjectPathId="9">
<Parameters>
<Parameter Type="String">AssignedTo</Parameter>
<Parameter Type="String">DEV\Administrator; DEV\spuser</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="15" ObjectPathId="9" />
<Query Id="16" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
<Property Name="AssignedTo" ScalarProperty="true" />
</Properties>
</Query>
</Query>
</Actions>
<ObjectPaths>
<StaticProperty Id="0" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
<Property Id="2" ParentId="0" Name="Web" />
<Property Id="4" ParentId="2" Name="Lists" />
<Method Id="6" ParentId="4" Name="GetByTitle">
<Parameters>
<Parameter Type="String">Project Tasks</Parameter>
</Parameters>
</Method>
<Method Id="9" ParentId="6" Name="GetItemById">
<Parameters>
<Parameter Type="Number">42</Parameter>
</Parameters>
</Method>
</ObjectPaths>
</Request>

Result:
Request failed. Invalid data has been used to update the list item. The field you are trying to update may be read only. 


<!-- Test 5
[ "AssignedTo", 1 ]
XML sent to Server using a single user id.  Works as a string as well.
-->

<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="14.0.0.0" LibraryVersion="14.0.4762.1000" ApplicationName="Javascript Library">
<Actions>
<ObjectPath Id="1" ObjectPathId="0" />
<ObjectPath Id="3" ObjectPathId="2" />
<ObjectPath Id="5" ObjectPathId="4" />
<ObjectPath Id="7" ObjectPathId="6" />
<ObjectIdentityQuery Id="8" ObjectPathId="6" />
<ObjectPath Id="10" ObjectPathId="9" />
<Method Name="ParseAndSetFieldValue" Id="11" ObjectPathId="9">
<Parameters>
<Parameter Type="String">Title</Parameter>
<Parameter Type="String">2013-02-11T20:37:00Z</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="12" ObjectPathId="9" />
<Query Id="13" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
</Properties>
</Query>
</Query>
<Method Name="ParseAndSetFieldValue" Id="14" ObjectPathId="9">
<Parameters>
<Parameter Type="String">AssignedTo</Parameter>
<Parameter Type="Number">1</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="15" ObjectPathId="9" />
<Query Id="16" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
<Property Name="AssignedTo" ScalarProperty="true" />
</Properties>
</Query>
</Query>
</Actions>
<ObjectPaths>
<StaticProperty Id="0" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
<Property Id="2" ParentId="0" Name="Web" />
<Property Id="4" ParentId="2" Name="Lists" />
<Method Id="6" ParentId="4" Name="GetByTitle">
<Parameters>
<Parameter Type="String">Project Tasks</Parameter>
</Parameters>
</Method>
<Method Id="9" ParentId="6" Name="GetItemById">
<Parameters>
<Parameter Type="Number">42</Parameter>
</Parameters>
</Method>
</ObjectPaths>
</Request>

Result:
Holy shit! It works....


<!-- Test 6
[ "AssignedTo", [ 1, 17 ] ]
XML sent to Server using an array of numbers.
-->

<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="14.0.0.0" LibraryVersion="14.0.4762.1000" ApplicationName="Javascript Library">
<Actions>
<ObjectPath Id="1" ObjectPathId="0" />
<ObjectPath Id="3" ObjectPathId="2" />
<ObjectPath Id="5" ObjectPathId="4" />
<ObjectPath Id="7" ObjectPathId="6" />
<ObjectIdentityQuery Id="8" ObjectPathId="6" />
<ObjectPath Id="10" ObjectPathId="9" />
<Method Name="ParseAndSetFieldValue" Id="11" ObjectPathId="9">
<Parameters>
<Parameter Type="String">Title</Parameter>
<Parameter Type="String">2013-02-11T20:40:36Z</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="12" ObjectPathId="9" />
<Query Id="13" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
</Properties>
</Query>
</Query>
<Method Name="ParseAndSetFieldValue" Id="14" ObjectPathId="9">
<Parameters>
<Parameter Type="String">AssignedTo</Parameter>
<Parameter Type="Array">
<Object Type="Number">17</Object>
<Object Type="Number">1</Object>
</Parameter>
</Parameters>
</Method>
<Method Name="Update" Id="15" ObjectPathId="9" />
<Query Id="16" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties>
<Property Name="Title" ScalarProperty="true" />
<Property Name="AssignedTo" ScalarProperty="true" />
</Properties>
</Query>
</Query>
</Actions>
<ObjectPaths>
<StaticProperty Id="0" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
<Property Id="2" ParentId="0" Name="Web" />
<Property Id="4" ParentId="2" Name="Lists" />
<Method Id="6" ParentId="4" Name="GetByTitle">
<Parameters>
<Parameter Type="String">Project Tasks</Parameter>
</Parameters>
</Method>
<Method Id="9" ParentId="6" Name="GetItemById">
<Parameters>
<Parameter Type="Number">42</Parameter>
</Parameters>
</Method>
</ObjectPaths>
</Request>

Result:
Well, it doesn't fail... However the column is set to null.


Well that just sucks now doesn't it?  Using that method, I can ONLY use the user ID AND ONLY one user ID.  What a waste of time.  The method should really be called: parseValuesAndWasteMatthewsTime().

What about GetList?

I could query the current site, cache all of the list information and then figure out the column types on the fly.
While I know I could pull this off, I really don't think it's a great option.  CSOM is asynchronous in nature, which means I'd have to nest everything AFTER the initial call to get the list information.  That would be worse than what is already there...

What's next?

Since this inefficiency cannot be overcome in SP2010, I'm going to press on with the API I currently have because it simply works...  I'll continue to look for better ways to do this, as this is primarily why I've never documented spUtils.  I figured the API would change once I found a different way and didn't want to deal with the overhead of dealing with that.

Well since I feel like I've given the SP Namespace a fair shake, it's time to clean up the code and start vetting it for usage within SP2013.  Look for much more capabilities to be baked into this coming soon!

Example: spUtils.startWorkflow();  // This will not use web services to accomplish this. I'm trying to rely on SP.js for everything.

Happy coding!

Friday, February 8, 2013

Debugging CSOM (Client Object Model) Client-Side

If you didn't get the pleasure of attending @xenoxg's session at the FEDSPUG last night, you definitely missed some very noteworthy information.  Definitely check it out if you can...

Xenox asked a simple question last night: How many people in the room use Fiddler?
I quickly raised my hand because I have used this many times as well as many other people in the room.

After he showed us the network traffic that was produced from his application, I thought to myself: When was the last time I used Fiddler?  I couldn't answer that question...  I've used this amazing tool, but for some reason I didn't know the last time I actually used it.  The main reason for this is because I live in browsers every waking moment...  The tooling has become quite good on the front-end of the house and I've actually replaced my need of Fiddler with the tools baked into them.

Behold!

Internet Explorer Developer Tools: Network

Setup a Network Capture

IE Dev Tools Network Tab
If you are unfamiliar with the developer tools, just press F12 when you have IE open.  If you have IE9+ (as you should!), you'll see the lovely Network tab.  It's extremely simple to use and gives me all the info that I need.  All you have to do is press the "Start Capturing" button and then render your page.  Here's an example of the types of information you'll typically see.




Since this is a post about CSOM, the relevant line to us is already highlighted.  Notice the Javascript makes a call to http://pathToServer/_vti_bin/client.svc/ProcessQuery.  Once you've found this in the captured network traffic, you simply highlight that particular line and click "Go to detailed view".  This button is located directly underneath the Network tab.

Viewing the Server Response


I'm not going to pretend to know all of what this stuff means, but it is nice to look through it and learn more about how this all is glued together for us.  The two tabs here that I use the most are "Request Body" and "Response Body".  If you are big into the now deprecated web services, you can think of the "Request Body" as your SOAP envelope.  In fact under the hood, it's actually still sending XML to the server:

<!-- START CODEZ -->
<Request xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009" SchemaVersion="14.0.0.0" LibraryVersion="14.0.4762.1000" ApplicationName="Javascript Library">
<Actions>
<ObjectPath Id="1" ObjectPathId="0" />
<ObjectPath Id="3" ObjectPathId="2" />
<ObjectPath Id="5" ObjectPathId="4" />
<ObjectPath Id="7" ObjectPathId="6" />
<ObjectIdentityQuery Id="8" ObjectPathId="6" />
<ObjectPath Id="10" ObjectPathId="9" />
<Query Id="11" ObjectPathId="9">
<Query SelectAllProperties="false">
<Properties />
</Query>
<ChildItemQuery SelectAllProperties="false">
<Properties>
<Property Name="ID" SelectAll="true" />
<Property Name="FileRef" SelectAll="true" />
<Property Name="FSObjType" SelectAll="true" />
<Property Name="FolderChildCount" SelectAll="true" />
<Property Name="ItemChildCount" SelectAll="true" />
<Property Name="Title" SelectAll="true" />
<Property Name="Something01" SelectAll="true" />
<Property Name="Something02" SelectAll="true" />
<Property Name="Something03" SelectAll="true" />
</Properties>
</ChildItemQuery>
</Query>
</Actions>
<ObjectPaths>
<StaticProperty Id="0" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" />
<Property Id="2" ParentId="0" Name="Web" />
<Property Id="4" ParentId="2" Name="Lists" />
<Method Id="6" ParentId="4" Name="GetByTitle">
<Parameters>
<Parameter Type="String">My List Name</Parameter>
</Parameters>
</Method>
<Method Id="9" ParentId="6" Name="GetItems">
<Parameters>
<Parameter TypeId="{3d248d7b-fc86-40a3-aa97-02a75d69fb8a}">
<Property Name="FolderServerRelativeUrl" Type="String">/Lists/SomeListName</Property>
<Property Name="DatesInUtc" Type="Boolean">true</Property>
<Property Name="ViewXml" Type="String">&lt;View&gt;&lt;Query&gt;&lt;OrderBy&gt;&lt;FieldRef Name='Something03' Ascending='True' /&gt;&lt;/OrderBy&gt;&lt;/Query&gt;&lt;/View&gt;</Property>
<Property Name="ListItemCollectionPosition" Type="Null" />
</Parameter>
</Parameters>
</Method>
</ObjectPaths>
</Request>
<!-- END CODEZ -->

The "Response Body" comes to us in the form of JSON:

// START CODEZ
[
{
"SchemaVersion":"14.0.0.0","LibraryVersion":"14.0.4762.1000","ErrorInfo":null
},
1,
{
"IsNull":false
},
3,
{
"IsNull":false
},
5,
{
"IsNull":false
},
7,
{
"IsNull":false
},
8,
{
"_ObjectIdentity_":"740c6a0b-85e2-48a0-a494-e0f1759d4aa7:web:90a9005d-7dcd-4784-9ed0-b4f722941dc0:list:f832844d-5ed1-4e39-98f3-d39f5e37ea3b"
},
10,
{
"IsNull":false
},
11,
{
"_ObjectType_" : "SP.ListItemCollection",
"_Child_Items_": [
{
"_ObjectType_":"SP.ListItem","_ObjectIdentity_":"740c6a0b-85e2-48a0-a494-e0f1759d4aa7:web:90a9005d-7dcd-4784-9ed0-b4f722941dc0:list:f832844d-5ed1-4e39-98f3-d39f5e37ea3b:item:43,1","_ObjectVersion_":"3","ID":43,"FileRef":"\u002fLists\u002fSomethingNav\u002fTest3","FSObjType":"1","FolderChildCount":"5","ItemChildCount":"0","Title":"Test3","Something01":null,"Something02":null,"Something03":1
},
{
"_ObjectType_":"SP.ListItem","_ObjectIdentity_":"740c6a0b-85e2-48a0-a494-e0f1759d4aa7:web:90a9005d-7dcd-4784-9ed0-b4f722941dc0:list:f832844d-5ed1-4e39-98f3-d39f5e37ea3b:item:1,1","_ObjectVersion_":"3","ID":1,"FileRef":"\u002fLists\u002fSomethingNav\u002fTest","FSObjType":"1","FolderChildCount":"4","ItemChildCount":"0","Title":"Test","Something01":null,"Something02":null,"Something03":2
},
{
"_ObjectType_":"SP.ListItem","_ObjectIdentity_":"740c6a0b-85e2-48a0-a494-e0f1759d4aa7:web:90a9005d-7dcd-4784-9ed0-b4f722941dc0:list:f832844d-5ed1-4e39-98f3-d39f5e37ea3b:item:22,1","_ObjectVersion_":"3","ID":22,"FileRef":"\u002fLists\u002fSomethingNav\u002fTest2","FSObjType":"1","FolderChildCount":"5","ItemChildCount":"0","Title":"Test2","Something01":null,"Something02":null,"Something03":3
}
]
}

// END CODEZ

Making Sense of it All

Let's get one thing clear, if you can make sense of this "Response Body" and why it's formulated the way that is it, you deserve a raise.  This portion of JSON that starts to make sense is directly after the 11.  Knowing what your JSON looks like can make it real easy to not only debug your scripts, but also definitively prove your script is communicating with the server correctly.  With this at your fingertips, there's no real need for me to use another piece of software.  And that's why I couldn't remember the last time I used Fiddler.  My browser(s) do everything I was using Fiddler for.  I'm definitely not saying it's a replacement, however for my needs, the browser suffices.  

Happy coding!

Tuesday, August 28, 2012

From the depths of SharePoint's sp.js Pt. 2

So I lied... :)  The function found below is a part of core.js.

A good ol' fashioned
console.dir( GetCurrentCtx().clvp );
generates this bit of awesome:

{

 ctx : [object Object],

 tab : [object],

 pagingTab : null,

 tBody : null,

 wpq : "WebPartWPQ4",

 inplUrl : "http://portal/SPDev/_layouts/inplview.aspx?List={49AFC299-FC30-4EE9-9571-50B3B366969E}&View={876230CC-BE1A-4845-8998-652BB77C891C}&ViewCount=7&ListViewPageUrl=http://portal/SPDev/pages/spUtils.aspx&IsXslView=TRUE",

 trEmpty : null,

 rootFolder : null,

 rootFolderGuid : null,

 rgpag : null,

 wpid : null,

 isEditing : false,

 dsrc : null,

 isInserting : false,

 strGroupName : null,

 strGroupCache : null,

 bRequestOutstanding : false,

 isEcbInfo : false,

 queueEcbInfo : ,

 fnEcbCallback : null,

 rgpaging : ,

 focusInfo : null,

 CUIItemInfo : [object Object],

 Init : function CLVPInit(){ULSiIp:;var f=this.ctx.listBaseType==1,d;if(this.ctx.listTemplate=="109"){this.ctx.clvp=null;return}d=this.ctx.listName+"-"+this.ctx.view;var b=GetElementsByName(d);if(b.length==0&&f){d="onetidDoclibViewTbl0";b=GetElementsByName(d)}va,

 RestoreNavigation : function CLVPRestoreNavigation(){ULSiIp:;var a=window.location.hash,c=this.InplViewUrl(),b=this.RefreshCurrent(true);b=b.substr(c.length+1);if(a.indexOf("InplviewHash=")==1){a=DecodeHashAsQueryString(a);var d=a.substr(14,38);if(d!=this.ctx.view)return;a=,

 FindWebPartDiv : function CLVPFindWebPartDiv(b){ULSiIp:;var a=b;while(a!=null&&a.tagName!="DIV")a=a.parentNode;return a},

 SyncPagingTables : function CLVPSyncPagingTables(){ULSiIp:;var b=this.pagingTab,a=document.getElementById("topPagingCell"+this.wpq.substr(7));if(b==null){if(a!=null)a.style.display="none";return}if(a!=null){a.style.display="";if(GetInnerText(a)!=GetInnerText(b))a.innerHTML,

 RehookPaging : function CLVPRehookPaging(){ULSiIp:;if(typeof this.ctx.noAJAX!="undefined"&&this.ctx.noAJAX)return;var tab=this.pagingTab;if(tab==null){this.SyncPagingTables();return}for(var lnks=tab.getElementsByTagName("A"),i=0;i<lnks.length;i++){var lnk=lnks[i],str=",

 FilterString : function CLVPFilterString(){ULSiIp:;if(this.rgpaging==null)return null;var a,d,b=[],c=true;for(a in this.rgpaging)if(a.indexOf("Filter")==0||a.indexOf("Sort")==0){if(c)c=false;else b.push("&");b.push(a);b.push("=");b.push(this.rgpaging[a])}return b.join(,

 RefreshPaging : function CLVPRefreshPaging(c,a,b){ULSiIp:;if(a!=null)this.tBody=document.getElementById(a);var d=this.isEditing?"JsGrid":null;return this.RefreshPagingEx(c,true,d,b)},

 RefreshPagingEx : function CLVPRefreshPagingEx(e,q,p,t){ULSiIp:;var s=GetUrlKeyValue("PageFirstRow",false,e)=="1";if(s)e=RemoveOnlyPagingArgs(e);var v=new CUrl(e),h=v.query;if(h.length>0)h=h.substr(1);var i=h.split("&"),f,a=[];if(p!=null)a.push("&Cmd="+p);else this.isEdit,

 ResetSelection : function CLVPResetSelection(){ULSiIp:;var a=this.ctx,f=true;this.CUIItemInfo=null;if(CountSelectedItems(a)>0){var h=a.dictSel;a.dictSel=[];a.CurrentSelectedItems=0;var g=window.location.href;if(g.indexOf("Filter")==-1&&g.indexOf("Sort")==-1){var d=this.t,

 WebPartId : function CLVPWebPartId(){ULSiIp:;if(this.wpid==null){if(this.tab!=null){var a=this.tab;a=GetAncestor(a,"DIV");if(a!=null){if(a.getAttribute("WebPartID2")!=null){this.wpid=a.getAttribute("WebPartID2");return this.wpid}if(a.getAttribute("WebPartID")!=null),

 RefreshCore : function CLVPRefreshCore(strUrl){ULSiIp:;MenuHtc_hide();var req;if(window.XMLHttpRequest)req=new XMLHttpRequest;else req=new ActiveXObject("Microsoft.XMLHTTP");var additionalPostData="";if(typeof this.ctx.overrideSelectCommand!="undefined"){additionalPos,

 RefreshCurrent : function CLVPRefreshCurrent(){ULSiIp:;var a=document.URL;STSNavigate(a)},

 GetQueryString : function CLVPGetQueryString(){ULSiIp:;if(this.tab!=null&&this.tab.getAttribute("FilterLink")!=null)return this.tab.getAttribute("FilterLink");else return document.URL},

 RefreshEcbInfo : function CLVPRefreshEcbInfo(b){ULSiIp:;var a=this.GetQueryString();a=FixUrlFromClvp2(this,a,false);this.isEcbInfo=true;this.strGroupName=b;var c=this;this.RefreshPagingEx(a,false,"EcbView")},

 CacheEcbInfo : function CLVPCacheEcbInfo(b){ULSiIp:;if(this.ctx.HasRelatedCascadeLists==1&&this.ctx.CascadeDeleteWarningMessage==null){var j="<CascadeDeleteWarningMessage>",k="</CascadeDeleteWarningMessage>";if(b.startsWith(j)){var g=b.indexOf(k);if(g!==-1){this.ctx.Ca,

 EnsureEcbInfo : function CLVPEnsureEcbInfo(c,f,a){ULSiIp:;var g="ecbtab_ctx"+this.ctx.ctxId,b=document.getElementById(g);if(b==null&&this.tab!=null&&this.tab.parentNode==null)for(var d=0;d<this.tab.childNodes.length;d++){var e=this.tab.childNodes[d];if(e.id==g)b=e}if(b&,

 InvalidateEcbInfo : function CLVPInvalidateEcbInfo(){ULSiIp:;var b="ecbtab_ctx"+this.ctx.ctxId,a=document.getElementById(b);if(a!=null){this.tab.removeChild(a);this.DeleteGroupNameCache()}},

 GetEcbInfo : function CLVPGetEcbInfo(b){ULSiIp:;var a=this.EnsureEcbInfo();if(a!=null)if(a.dict!=null)return a.dict[b];return null},

 SwitchToEdit : function CLVPSwitchToEdit(){ULSiIp:;var a=document.URL;a=FixUrlFromClvp2(this,a,false);this.RefreshPagingEx(a,false,"JsGrid")},

 EnsureChangeContext : function CLVPEnsureChangeContext(){ULSiIp:;if(this.cctx==null)this.cctx=new SP.ClientContext(null)},

 DeleteItemCore : function CLVPDeleteItemCore(c,f){ULSiIp:;var h=this.ctx.listBaseType==1;this.EnsureChangeContext();var a=null;if(typeof this.rgehs=="undefined")this.rgehs=[];var b,e;b=new SP.ExceptionHandlingScope(this.cctx);this.rgehs.push(b);e=b.startScope();this.cctx,

 CheckoutItem : function CLVPCheckoutItem(f,d){ULSiIp:;this.EnsureChangeContext();if(typeof this.rgehs=="undefined")this.rgehs=[];var a,c,b=null;if(d!=0)return;a=new SP.ExceptionHandlingScope(this.cctx);this.rgehs.push(a);c=a.startScope();b=this.cctx.get_web().get_lists,

 DiscardCheckoutItem : function CLVPDiscardCheckoutItem(e,g){ULSiIp:;this.EnsureChangeContext();if(typeof this.rgehs=="undefined")this.rgehs=[];var b=null,a,f;if(g!=0)return;a=new SP.ExceptionHandlingScope(this.cctx);this.rgehs.push(a);f=a.startScope();b=this.cctx.get_web().ge,

 CheckinItem : function CLVPCheckinItem(g,j,a){ULSiIp:;this.EnsureChangeContext();if(typeof this.rgehs=="undefined")this.rgehs=[];var c=null,b,h;if(j!=0)return;b=new SP.ExceptionHandlingScope(this.cctx);this.rgehs.push(b);h=b.startScope();c=this.cctx.get_web().get_list,

 ManageCopies : function CLVPManageCopies(f,e){ULSiIp:;if(e!=0)return;var b=this.GetEcbInfo(f);if(b!=null&&b.getAttribute("Url")!=null){var d=this.ctx.HttpRoot+"/_layouts/managecopies.aspx?ItemUrl="+b.getAttribute("Url")+"&Source="+GetSource(),a=window["ctx"+this.ctx.ct,

 ShowErrorDialog : function CLVPShowErrorDialog(g){ULSiIp:;var i,a=[],d=null,c=false;for(i in this.rgehs){var b=this.rgehs[i];if(b.get_hasException()){var f="s4-dlg-err-itm";if(!c){f="s4-dlg-err-firstitm";c=true;a.push(SP.Res.dlgTitleError)}var k=b.get_errorMessage();a.pus,

 InplViewUrl : function CLVPInplViewUrl(){ULSiIp:;if(this.inplUrl!=null)return this.inplUrl;var a=[],b=escapeUrlForCallback(this.ctx.HttpRoot);a.push(b);b[b.length-1]!="/"&&a.push("/");a.push("_layouts/inplview.aspx?List=");a.push(this.ctx.listName);if(this.ctx.view!=n,

 InplViewUrlTrim : function CLVPInplViewUrlTrim(a){ULSiIp:;var b="";if(a)if(typeof this.ctx.overrideFilterQstring!="undefined")b=a.substr(this.InplViewUrl().length-this.ctx.overrideFilterQstring.length);else b=a.substr(this.InplViewUrl().length+1);return b},

 ShowPopup : function CLVPShowPopup(a){ULSiIp:;MenuHtc_hide();if(typeof this.fnOnCommitPopup!="undefined"&&this.fnOnCommitPopup!=null)OpenPopUpPage(a,this.fnOnCommitPopup);else OpenPopUpPage(a,RefreshPage);return false},

 ValidateField : function CLVPValidateField(e,d,a,b){ULSiIp:;var c=this.FieldCommand("FldValidate",e,d,a,b),f={callback:a,newval:b,strCmd:c};if(this.qvalidate==null)this.qvalidate=[];this.qvalidate.push(f);if(!this.isValidating){this.isValidating=true;this.RefreshCore(c),

 FieldCommand : function CLVPFieldCommand(j,h,d,m,f){ULSiIp:;var a=[];a.push(this.InplViewUrl());a.push("&Cmd=");a.push(j);a.push("&Field=");a.push(d);a.push("&FieldValue=");var b=h.GetProp(d),c,n=this.gridParam.tableViewParams.columns.GetColumnByKey(d),k=jsGridParams.t,

 IsInGroupCache : function CLVPIsInGroupCache(a){ULSiIp:;if(this.strGroupCache==null)return false;return this.strGroupCache.indexOf(a+"$")!=-1},

 CacheGroupName : function CLVPCacheGroupName(a){ULSiIp:;if(this.strGroupCache==null)this.strGroupCache=a+"$";else this.strGroupCache=this.strGroupCache+a+"$"},

 DeleteGroupNameCache : function CLVPDeleteGroupNameCache(){ULSiIp:;if(this.strGroupCache==null)return;var b=0,a;a=this.strGroupCache.indexOf("$",b);while(a!=-1){var d=this.strGroupCache.substring(b,a),e="ecbtab_ctx"+this.ctx.ctxId+"_"+d,c=document.getElementById(e);c&&this.tab,

 EnqueueEcbInfoRequest : function CLVPEnqueueEcbInfoRequest(b){ULSiIp:;for(var a=0;a<this.queueEcbInfo.length;a++)if(this.queueEcbInfo[a]==b)return;this.queueEcbInfo.push(b)},

 NoOutstandingECBRequests : function CLVPNoOutstandingECBRequests(){ULSiIp:;if(this.isEcbInfo)return false;if(this.queueEcbInfo.length>0)return false;return true}

}

That makes a bunch of noise, however, you'll see an interesting URL at the property named: inplUrl. 
Upon visiting this URL, you'll see all of your data for that particular view.  This seems very attractive for AJAX applications in SharePoint.  Look for more to come in regards to this...

Cheers,
Matthew