Skip to main content

Posts

Get User Id using REST or JavaScript Object Model

Sometimes you would need to fetch the User Id based on either Login name or Email id. You would need User Id if you need to assign a user object to a people picker control or People/Group field.

How do we get the Id based on Email or Login Name in client side development? We can achieve that using JavaScript Object Model or REST API. Let me share the first example using JavaScript Object Model (JSOM).

var context = new SP.ClientContext.get_current();
this.user = context.get_web().ensureUser(loginName or Email);

var o = { d: d, user: this.user };

context.load(this.user);
context.executeQueryAsync(
    Function.createDelegate(o, ensureUserSuccess),
    Function.createDelegate(o, Fail)
);

The above code fetches User Id for a given Login Name or Email Id.

Interestingly, there is no equivalent endpoint available in REST! The Microsoft documentation talk about a endpoint but I could not get it working.

So what is the way to get User Id using REST? You have to use the hidden User Information…
Recent posts

Maintaining Multi-line text format in Angular

In my earlier post, I had discussed about maintaining multi-line text format in AngularJS view. In this post, I'll share how to achieve the same in Angular. What I have described in this post is tested in Angular 7.

To provide a context, let us say you are creating a page using Angular and rendering data from a SharePoint list/library. One of the columns is multiple lines of text with rich text enabled. The objective is to display the rich text as it is.

In AngularJS, we created a custom filter and used that filter in the HTML element.

In Angular, it is pretty easy. Angular already provides [innerHTML] property to handle such cases. Let's look at an example.

<p [innerHTML]="description"></p>

Here, "description" is a property in Angular component which holds the value of a multi-line text column.

If you are thinking if it is safe to use this, check the Angular documentation on security (yes, it is safe!)

Update on Posts and Comments

I have seen many comments in last several months asking questions on some of the posts. First of all, I apologies for not responding on time. I had set email to receive notification for new comments but for some weird reasons, I was not getting mails at all. Since I did not check for new comments in the site itself, I did not even know that there were comments, so I could not respond to your questions.

I'm trying to fix this notification issue. Meanwhile, I'll keep checking in the site itself for any new comments.

Also I have observed, there were plenty of spam comments in the site. I had kept posting comments open but looks like spammers have taken the advantage of it for their product/service marketing. It took quite a bit of time to individually delete all the spam comments. Going forward, all the comments will be moderated. That means, it will not be published immediately. I hate to do this but this is the only way to filter spam comments. I'll also make sure to check…

UnauthorizedAccessException while checking If user is part of a Group

To check if a user is part of a group programmatically, we can use this end point:

$.ajax({ 

   type: "GET", 
   url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('groupName')/users?$filter=Id eq " + userId, 

   headers: {"accept": "application/json; odata=verbose"}, 
   success: function(data){
     // Success logic 
   }, 
   error: function(error){ 
     alert("Error"); 
     console.log(error); 
   } 

 }); 

When you execute this code, and (if) you get the below error

Access denied. You do not have permission to perform this action or access this resource
401
System.UnauthorizedAccessException
Navigate to the group, go to Group settings, and ensure "Who can view the membership of the group?" to "Everyone".

By default, only the Group Owner can view the membership of a Group. That means, the above code works if the logged in user executing the code is the group owner. For everyone else, it throws…

Announcement of SharePoint 2019

The last month, Microsoft announced the strategy, vision, and timeline for SharePoint 2019.

From the screenshots provided, it is evident User Experience has improved. Though the article has listed the key features, we have to wait to experience what changes SharePoint 2019 brings to the users.

SharePoint 2019 is available to the customers in second half of this year. Preview should be available to the customers soon.

Check this article for details.

Filter on Date column of a Calendar List using REST API

In SharePoint 2013 on-premises site, if you are trying to apply a filter on EndDate column (or EventDate column) of a Calendar List using REST API, it throws the below error.

The field 'EndDate' of type 'DateTime' cannot be used in the query filter expression.
Basically we cannot use EndDate column in the filter query. That sounds weird but it looks to be a "miss" from the SharePoint product team.

However, it is interesting to note that this filter works fine in SharePoint Online (SPO). So the sample query like below returned valid items.

https://domain/sites/pub/_api/web/lists/getByTitle('Calendar')/items?$select=Title,EndDate&$filter=EndDate lt '2018-05-31T23:59:00Z'

So, the Product team might have fixed the issue in SPO. Hope this should be resolved at least in SP2016.

Send items to Recycle Bin using REST

When we have to implement Delete operation using REST API, we normally use the straightforward method to delete an item or a document. For example, to delete an item from a List, we use the following code.

$.ajax({ 
 type: "POST", 
 url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('CustomList')/items(2)", 

 headers: { 
   "accept": "application/json; odata=verbose",
     "X-HTTP-METHOD": "DELETE",
     "IF-MATCH": "*",
     "X-RequestDigest": $("#__REQUESTDIGEST").val()
 },

 success: function(data){
     alert("Item is deleted!");
 },
 error: function(error){
     alert("Error"); console.log(error);
 }

});

To delete a document from a Library, we use the below code.

$.ajax({ 
 type: "POST", 
 url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/getfilebyserverrelativeurl('/sites/pub/Documents/DocIcon.png')"…