Thursday 17 October 2013

Users vs AllUsers vs SiteUsers in SharePoint

Hi All,

Today we will see about the Usercollections in SharePoint.Lets we see the Scenario

Scenario:

You want to get all the users who have access to the site. An SPWeb object exposes three different collections of users, as shown in this code below.

Code:

SPWeb web = SPContext.Current.Web;

SPUserCollection c1 = web.Users;

SPUserCollection c2 = web.AllUsers;

SPUserCollection c3 = web.SiteUsers;



Ok.... So what is the difference among these 3 collections.

The "Users" collection has the smallest membership of these three collections. This collection includes all the external principals that have been explicitly assigned permissions within the current site.


The "AllUsers" collection includes all members of the Users collection, plus external users that have accessed objects within the site using implicit permissions through group or role membership. For example, imagine a user named Sachin with the login of Company\Sachin that has never been given explicit permissions to access a site and view a particular list. However, he might still be able to view the list because of his membership within an Active Directory group that has been configured with list view permissions. When Sandeep first accesses the site or one of its objects (say, a list using implicit permissions), he is added as a member of the AllUsers collection, but he is not added as a member of the Users collection.

The "SiteUsers" collection is an aggregation that combines membership for each AllUsers collection within the current site collection. The membership of this collection includes all external principals that have been assigned permissions to any object within the site collection as well as all external users that have been granted access to any of the site collection's objects using implicit permissions.

I hope now u will get an idea about these collections.

Obviously there, u will get this doubt.


-----What will the SharePoint UserInfoList contain - AllUsers or SiteUsers or Users?

And the answer is, Actually it contains cached information about the users logged in at least once to the site collection.

See u on my next post :) byeee....


Wednesday 2 October 2013

DateTime Fields and CAML Queries , OffsetDays...

Hi Friends,
 Today I will share you some tips regarding CAML queries in SharePoint. Most of you would have played with CAML Queries.
OK , Now I will tel you how to deal with the Dates in CAML Query.

Scenario 1 : If you are checking for the items having "Due Date" field value as "Today", then this would be the query for u.

<Where>
<Eq>
<FieldRef Name="DueDate" />
<Value Type="DateTime">
<Today/>
</Value>
</Eq>
</Where>

Scenario 2 : what if u want something dynamic???
 EX: if u want to get the items, having "Due Date" 5 days before Today. For this you can't give a static day, because "Today" changes daily.
     Option is "OffsetDays".
The Query goes like this :

<Where>
<Eq>
<FieldRef Name="DueDate" />
<Value Type="DateTime">
<Today OffsetDays="-5" />  ----------> for future dates the value should be positive.
</Value>
</Eq>
</Where>

In many documents they have mentioned Offset instead of "OffsetDays"... better be careful while checking...

Have a good Day :)