Filtering current user through webservice using [Me]? [Resolved]

Mar 2, 2010 at 5:26 PM
Edited Mar 2, 2010 at 9:03 PM

I'd like to filter items in a query based on the current logged in user. I see the UserContextFIlter.webpart but that wouldn't work with jPoint calls. I have seen CAML queries that would work if you have already added a field like AssignedTo and set that. I'd rather not have to add that to all the lists that would be accessed, and I'd rather not have to do that extra assignment either.

I saw a reference to _spUserID on a website and I see it in the DOM (via Firebug). If that is safe to use that could be used for a web service filter. But in SharePoint Designer it shows "Created By" as the HTML display version. I did see a CAML reference to FieldRef Name='Author', and also a separate site say that a list item has an Author and Editor field for the related "Created By" and "Modified By" fields. If that's so then I could filter a jPoint call with the following CAML

<Where><Eq><FieldRef Name='Author'/><Value Type='Integer'><UserID Type='Integer'/></Value></Eq></Where>

but that would mean building the CAML manually or using the buildQueryContent function.

... ah this would be simpler if I just used a view with a filter of [Me] wouldn't it? Wow that was easy. OK so really what I want for jPoint calls that use the current logged in user is to use a view of that data with the [Me] filter on it.

Does that sound right?

[edit] As another option, though it wouldn't filter items received from the web service, would it be possible to use the 'Author' and 'Editor' fields with filterItems?

[edit2] The more I think of it, long term, it might be neat to be able to arbitrarily add CAML clauses. In this particular context, for a developer using jPoint, it could be something like an additional property filterForCurrentUser and if so it'd pop in that Where clause.

There aren't any joins for SP 2007, but they are in there for 2010 it seems (yay!) and that would add extra complexity. Without the joins adding wheres should be *relatively* straightforward. It would get closer to being an ORM which might not be desirable so perhaps it could be something that is more hook based. Is there a way to modify a query without hacking the code? I haven't looked through the code enough yet but it seems that once you use one of the set functions it'll fire off your query with that but there isn't yet a way to modify it. I just saw the filterNode and filterAttr options but I haven't yet worked through what they mean or how they are used. But they are also post-call processing.

Just thinking out loud and capturing ideas on usage as I work through these things.

Thanks!

Michael

Coordinator
Mar 2, 2010 at 8:52 PM

 

<Query>
   <Where>
      <Eq>
         <FieldRef Name='Author' />
         <Value Type='User'>samir sijercic</Value>
      </Eq>
   </Where>
</Query>

Hi Michael,

SharePoint designer calls it "Created By" but internal SharePoint name is "Author" and it is of nvarchar value.

Now using U2UCaml builder looks like following CAML query would produce desirable results:

<Query>

   <Where>

      <Eq>

         <FieldRef Name='Author' />

         <Value Type='User'>display name</Value>

      </Eq>

   </Where>

</Query>

 

In order to get Display Name you can use "Title" field from  http://www.sharejpoint.com/examples/src/jPUserProfile.aspx example.

 

Coordinator
Mar 2, 2010 at 9:03 PM

OK even better solution:

 

<Query>
   <Where>
      <Eq>
         <FieldRef Name='Author' LookupId='TRUE'/>
         <Value Type='Integer'>3503</Value>
      </Eq>
   </Where>

</Query>

 

var myQuery = "<Where><Eq><FieldRef Name='Author' LookupId='TRUE'/><Value Type='Integer'>" + _spUserID + "</Value></Eq></Where>";

Then you call ListOject.getSPItemsWithQuery(myQuery);

Then process results.

 

Mar 2, 2010 at 9:17 PM

Ah that's right I can set the list first and do my filtering off of that. So the functionality is already there. Much simpler. Thanks! :) This makes me want to do more SharePoint in the future. I could see a plugin of helper functions for generating CAML.

And I saw a reference in the last couple hours about use LookupId='TRUE'.

Unfortunately I don't have access to a server to install the U2UCaml Builder Feature version. I would need the Windows version but the link is broken on the site. Looks like a good tool. I have download Stramit's CamlViewer but I haven't put it through its paces yet.

Thanks again!

Michael

Coordinator
Mar 2, 2010 at 9:35 PM

Once you get to download U2UCaml you can from your machine point to your server using webservice option to query lists and checkout list structure.

We had the similar idea of creating a plugin for generating CAML as online version along side jPoint, but it is pushed back for now.

If someone in community wants to tackle it we are all for it.

Mar 2, 2010 at 9:44 PM
The CAML structure makes for interesting coding. :) But it would be good. Fortunately it can be done outside of core code as plugins so that can help. This is my first SharePoint project but if I have more it would be worth my time to help I think.

Unfortunately it seems that the Windows version of U2UCaml has been put aside for now as some of the updates to the Feature version aren't so easily ported back to the Windows version. I haven't been able to find a download for the Windows version yet.

samirsijercic wrote:

From: samirsijercic

Once you get to download U2UCaml you can from your machine point to your server using webservice option to query lists and checkout list structure.

We had the similar idea of creating a plugin for generating CAML as online version along side jPoint, but it is pushed back for now.

If someone in community wants to tackle it we are all for it.

Coordinator
Mar 2, 2010 at 9:52 PM

 

This should work
http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx

This should work

http://www.u2u.be/Res/Tools/CamlQueryBuilder.aspx

 

Mar 2, 2010 at 10:04 PM
Ack! I never saw that page mentioned nor the fact that it reached version 4.0! (Yeah sure NOW when I do a google for u2u camlbuilder this page is at the top of the list... where was it the last two days! I must need sleep...) Many thanks!

samirsijercic wrote:

From: samirsijercic

Mar 2, 2010 at 10:38 PM
Very nice indeed! The only thing it doesn't allow is adding attributes to the CAML automatically, like LookupId='TRUE'. I can add it manually but it overwrites it with other changes so I'll just have to remember to pop that in if using an Author/Editor query. :) Thanks very much for the link.
Mar 3, 2010 at 4:31 PM
And this is good for those who write SQL:

http://www.spsprofessional.com/sqlcaml.aspx
Coordinator
Mar 3, 2010 at 5:09 PM

Nice tool for SQL translation!

Did not come across this tool before.

Nice Find!

Mar 3, 2010 at 5:32 PM

I just wish I could do a straight SQL query on this list table with joins. :( I would have had the data model done a while back. 

Coordinator
Mar 3, 2010 at 5:35 PM

If they made it that easy, we would have a lot more SharePoint developers, decreasing developer value, decreasing $ :-)