Wednesday, May 22, 2019

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 single-value person columns, all you have to do is set the value to -1.
It requires an integer value, so null or empty string will give you an error: Cannot convert a primitive value to the expected type ‘Edm.Int32′.
For multi-value person columns, you have to set the results value to an empty array:

Wednesday, March 13, 2019

Cannot convert the literal ‘1’ to the expected type ‘Edm.String’ - REST API

If getting this error make sure you are giving quotation marks to your input passed in the request.
JSON Data - 
data={name="John", age=30}
if you are expecting age as a string parameter then it return error saying can not convert the literal '30' to the expected type  ‘Edm.String’
to resolve this use below
data={ name="John", age="30"}
in your http request.

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);
 }
 }
 );
}

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 ...