Friday, August 10, 2018

Calendar View for List - Wrong Date Interval for End Date.

Calendar View for List - Wrong Date Interval for End Date -

Calendar View shows a day less because since the Only Date field also saves the time by default as 12:00 AM.

To avoid this,

Solutions -

1.  Either convert the Date only field to Date and Time field and ask the user to enter both date and time.

2. Using workflow, add a day more to the End Date.

3. Create a calculated column with return type as Date/Time and add a day more to the End Date
i.e.
Suppose you created a calculated column as,


Now, use the new calculated fields , Visitor's End Date as the new time interval as shown below,



Date formats in Custom Display Form SharePoint

Usually, when a custom display form for a SharePoint list is created having date time fields,

the form shows the date format as,

The format as shown above has a drawback that ,

1. Date field shows time value too.
2. Date field shows an hour less due to which there will occur a day less in the date field.

Solution:-

To avoid the above situation,

Modify the custom display form's xsl as -



Important Note :-

1. Use "string" function to avoid XPath error.
2. 1033 is a locale code.
3. 1 is the date format, the possible values can be 1,4,5,7,12,13,15. Each represents a date format for e.g. y/m/d OR y-m-d etc.

Wednesday, April 25, 2018

Get Users from AD Group added in SharePoint Group

Requirement - I want to get all members which are there in AD group (added in SP group).

In SharePoint If you add AD group then SharePoint will consider AD group as a single user instead of group. So you will not able to get users directly from AD group after adding in SharePoint groups.


Option 1: Use group membership visibility as a workaround
One potential work around is to exploit a combination of two properties that you can access on groups via the JavaScript client object model: OnlyAllowMembersViewMemberhip and CanCurrentUserViewMembership.
If the current user can view group membership for a group that is only set to allow group members to do so, we can assume the user is a group member.

var clientContext = new SP.ClientContext();
var groupId = 5; // the group membership ID for the group you want to check
var group = clientContext.get_web().get_siteGroups().getById(groupId);
clientContext.load(group,"CanCurrentUserViewMembership");
clientContext.load(group,"OnlyAllowMembersViewMembership");
clientContext.executeQueryAsync(
    function(sender,args){
        var isMemberOfGroup = group.get_canCurrentUserViewMembership() && group.get_onlyAllowMembersViewMembership();
        if(isMemberOfGroup){
            doSomething();
        }
    },
    function(sender,args){"Whoops! "+alert(args.get_message());}
);
This approach will only work if you've set the groups to only be visible to members, and it'll always return a false positive if you have elevated access, such as if you're a site collection administrator or the group owner.
Edit: How to Iterate Through All Site Groups
If you want to apply the same logic as above to check the current user's membership in all groups on the site (instead of specifying a group by its ID), you can use the modified JavaScript code below.
var clientContext = new SP.ClientContext();
var groups = clientContext.get_web().get_siteGroups()
clientContext.load(groups,"Include(CanCurrentUserViewMembership,OnlyAllowMembersViewMembership,Title)");
clientContext.executeQueryAsync(
function(sender,args){
    var groupIterator = groups.getEnumerator();
    var myGroups = [];
    while(groupIterator.moveNext()){
        var current = groupIterator.get_current();
        var isMemberOfGroup = current.get_canCurrentUserViewMembership() && current.get_onlyAllowMembersViewMembership();
        if(isMemberOfGroup){
            myGroups.push(current.get_title()); // this example adds group titles to an array
        }
    }
    alert(myGroups); // show the array
},function(sender,args){"Whoops! "+alert(args.get_message());});
Option 2: Use Audience Targeting as a workaround
Of course, as I mentioned in a comment, for your requirements you may not even need programmatic access to the group membership. You could just set audience targeting on the web parts that you want to be visible only to certain groups; audience targeting should respect AD group membership.

Option 3 : Using REST API 

function spjs_isCurrentUserInGroup(groupIdOrName){
var endpoint;
if(typeof groupIdOrName === "string"){
endpoint = _spPageContextInfo.webAbsoluteUrl+"/_api/web/sitegroups/getbyname('"+groupIdOrName+"')/CanCurrentUserViewMembership" 
 }else{
 endpoint = _spPageContextInfo.webAbsoluteUrl+"/_api/web/sitegroups("+groupIdOrName+")/CanCurrentUserViewMembership" 
 }
 return jQuery.ajax({ 
 "url":endpoint,
 "type":"GET", 
 "contentType":"application/json;odata=verbose",
 "headers":{ 
 "Accept": "application/json;odata=verbose"
 }
 });
}

function checkADGroupMembership(){
 spjs_isCurrentUserInGroup(18).success(
 function(data){
 if(data.d.CanCurrentUserViewMembership){
 setTimeout(function(){
 spjs.dffs.triggerRule(["isInADGroup"]);
 },10);
 }
 }
 );
}

Tuesday, January 2, 2018

Caching in jQuery getScript function

In SharePoint 2013, there is a jQuery.getScript method which is suggested for loading SP.Runtime.js and SP.js files on the page.

The implementation of jQuery.getScript looks like,

jQuery.getScript(_spPageContextInfo.webAbsoluteUrl+"_layouts/15/sp.runtime.js",function(){
   
      alert("SP.Runtime.js loaded");
});

jQuery.getScript(_spPageContextInfo.webAbsoluteUrl+"_layouts/15/sp.js",function(){
   
      alert("SP.js loaded");
});

Thus, this function came out as a good practice for loading such mandate files on the page but then there is a limitation to this,

1) jQuery.getScript  - fetches the fresh copy of the files every time the page is getting loaded with the current time stamp appended to the file path, because this method disables caching of the browser thus degrading the performance of the page load.

The browsers have a property to cache the file once it's fetched to improve page loads from the next time if the such files are requested.

How to overcome this limitation ?

According to further study on jQuery.getScript method on Microsoft,

I came to a conclusion that jQuery.getScript method calls an ajax function internally which has a "cache" parameter which we can set to "true" if we want the function to look into the browser cache for the requested file or "false" if we want the file to be loaded from the original location.

Now the function looks like,

jQuery.getCachedScript(url,callback){

        jQuery.ajax({
           
             datatype: 'script',
             cache: true,
             url: url,
             success: callback
        });

};

jQuery.getCachedScript(_spPageContextInfo.webAbsoluteUrl+"_layouts/15/sp.runtime.js",function(){
   alert("SP.Runtime got loaded");
});

So, now there is no time-stamp appended to the file path which is fetched on page load as caching is set to 'true'.


Clearing Person/Group Values

To clear out or empty the values in person or group columns, how you do it depends on if the column is single-value or multi-value. For ...