Thursday 14 December 2017

Check If Current logged in User is Part of SharePoint Group

SharePoint Online Environment:

var isHelpDeskUser = False;

$(function() {
ValidationForHelpDesk();
});


function ValidationForHelpDesk(){
var url = _spPageContextInfo.webAbsoluteUrl +'/_api/web/currentuser/groups'
    $.getJSON(url, function (data) {
        $.each(data.value, function (key, value) {
            if (value.Title == 'SAGroup') {    //SharePoint HelpDesk Group name is SAGroup         
              isHelpDeskUser = true;   
           }
        });
    })
}

If Current User is part of "SAGroup" then variable isHelpDeskUser will set to true;

SharePoint On Premise environment:

 function ValidationForHelpDesk(){
CurrentUserMemberOfGroup("SAGroup", function (isCurrentUserInGroup) {  
  if(isCurrentUserInGroup)  
  {     
     console.log('user is member of the group'); 
     isHelpDeskUser = true;
       
  }  
  else  
  {        
 console.log('user is not member of the group'); 
 isHelpDeskUser = false; 
 
  }
});
}//Function Close

function CurrentUserMemberOfGroup(groupName, OnComplete) {  
   
        var ctx = new SP.ClientContext.get_current();  
        var currentUser = ctx.get_web().get_currentUser();  
        ctx.load(currentUser);   
        var Groups = ctx.get_web().get_siteGroups();  
        ctx.load(Groups);   
        var group = Groups.getByName(groupName);  
        ctx.load(group);          
        var groupUsers = group.get_users();  
        ctx.load(groupUsers);
        ctx.executeQueryAsync(  
                function(sender, args) {  
                   var userInGroup = UserInGroup(currentUser,group);           
                   OnComplete(userInGroup);  
                },  
                function OnFailure(sender, args) {  
                   OnComplete(false);  
                }  
        );            
        function UserInGroup(user,group)  
        {  
            var groupUsers = group.get_users();  
            var userInGroup = false;  
            var groupUserEnumerator = groupUsers.getEnumerator();  
            while (groupUserEnumerator.moveNext()) {  
                var groupUser = groupUserEnumerator.get_current();  
                if (groupUser.get_id() == user.get_id()) {  
                    userInGroup = true;  
                    break;  
                }  
            }  
            return userInGroup;  
        }  
}

Sunday 22 January 2017

How to back up WSP from Central Administration

Hi,

Here is the way to get wsp file from SharePoint Central Administration.

Open SharePoint Management Shell(PowerShell) and run below script.

$farm = Get-SPFarm
$file = $farm.Solutions.Item("MyProject.wsp").SolutionFile
$file.SaveAs("c:\temp\MyProject.wsp")


Tuesday 6 December 2016

Read Custom User Property created in Central Admin using JavaScript

Hi All,

I have faced recently one problem. Created a custom user property in Central Admin and reading property using JSOM as below.
/**** Code Start****/
$(document).ready(function(){
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', CallClientOM);
});
function CallClientOM()
{
var context = new SP.ClientContext.get_current();
this.website = context.get_web();
this.currentUser = website.get_currentUser();
context.load(currentUser);
context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}
function onQuerySucceeded(sender, args)
{  
  get_user_profile_by_login(currentUser.get_loginName());
}

function get_user_profile_by_login(login) {
$().SPServices({
 operation: "GetUserProfileByName",
 async: false,
 AccountName: login,
 completefunc: function (xData, Status) {
Supervisor = getUPValue(xData.responseXML, "Supervisor");
$("input[title='Supervisor']").val(Supervisor);
  }
});
}
function getUPValue(x, p) {
  var thisValue = $(x).SPFilterNode("PropertyData").filter(function() {
    return $(this).find("Name").text() == p;
  }).find("Values").text();
  return thisValue;
}
/**** Code End****/

Problem: Here I have created Supervisor field as custom user property, but I am not able to read
from the code(as a non admin). I was set as, Policy Settings -> Default Privacy Setting -> Only Me.
Solution: While creating the property set as below:
Policy Settings -> Default Privacy Setting -> Everyone.
Then it worked!!!



SharePoint Timer Job Run Mechanisms

If you want to run it every TEN MINUTES,use SPMinuteSchedule class to configure:
SPMinuteSchedule minSchedule = new SPMinuteSchedule();
minSchedule.BeginSecond = 0;
minSchedule.EndSecond = 5;
minSchedule.Interval = 10;
tempJob.Schedule = minSchedule;
tempJob.Update();


If you want to run it at 11:05 PM every day, use SPDailySchedule class to configure:
SPDailySchedule tempSchedule = new SPDailySchedule();
tempSchedule.BeginHour = 23;
tempSchedule.BeginMinute=5;
tempSchedule.BeginSecond = 0;
tempSchedule.EndSecond = 15;
tempSchedule.EndMinute = 5;
tempSchedule.EndHour = 23;
tempJob.Schedule = schedule;
tempJob.Update();


If you want to run it on the 1st of every month between 1:15am to 1:30am, use SPMonthlySchedule
SPMonthlySchedule schedule = new SPMonthlySchedule();
schedule.BeginDay = 1;
schedule.EndDay = 1;
schedule.BeginHour = 1;
schedule.EndHour = 1;
schedule.BeginMinute = 15;
schedule.EndMinute = 30;
tempJob.Schedule = schedule;
tempJob.Update();

If you want to run on Monday every week between 2:01:00 am to 2:01:05 am, use SPWeeklySchedule
SPWeeklySchedule schedule = new SPWeeklySchedule();
schedule.BeginDayOfWeek = DayOfWeek.Monday;
schedule.BeginHour = 2;
schedule.BeginMinute = 1;
schedule.BeginSecond = 0;
schedule.EndSecond = 5;
schedule.EndMinute = 1;
schedule.EndHour = 2;
schedule.EndDayOfWeek = DayOfWeek.Monday;
tempJob.Schedule = schedule;
tempJob.Update();


If you want to run it every year on Jan 23 at 9:05AM, use SPYearlySchedule
SPYearlySchedule JobSchedule = new SPYearlySchedule();
JobSchedule.BeginMonth = 1;
JobSchedule.EndMonth = 1;
JobSchedule.BeginDay = 23;
JobSchedule.EndDay = 23;
JobSchedule.BeginHour = 9;
JobSchedule.EndHour = 9;
JobSchedule.BeginMinute = 5;
JobSchedule.EndMinute = 5;
JobSchedule.BeginSecond = 0;
JobSchedule.EndSecond = 5;
tempJob.Schedule = schedule;
tempJob.Update();

Owstimer.exe is the worker process to debug the code.

Wednesday 21 September 2016

Error occurred in deployment step ‘Activate Features’: Attempted to perform an unauthorized operation

I have created a SharePoint empty project and added visual webpart into it.
When I try to deploy the solution, ended up with below error.

"Error occurred in deployment step "Activate Features" attempted to perform an unauthorized operation"


Reason:
The account which you are trying to deploy the solution may not have permission to specific webapplication/SiteCollection.

Solution:
  • Open SharePoint 2013 Central Administration.
  • Navigate to Application Management and choose Change site collection administrations
  • Select the site collection where deployment should happen
  • Add you windows account[Account which you are trying to deploy] as the primary (or) secondary site collection administrator.

Also check account has permission to access database, if not please add into Security section of SQL Server.

Hope this helps!!!

Monday 13 June 2016

How to update URL field in SharePoint List using JSOM

 Here is the code to update URL field.
URL will have two properties. One is url and other is description.
Code below is using JSOM.
======================================================
var itemCreateInfo = new SP.ListItemCreationInformation();
var rootUrl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;
itemCreateInfo.set_folderUrl(rootUrl + "/Lists/PROJECTS/Folder");//Create item folder inside list
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('NUMBER', "23");
     var fvalue = new SP.FieldUrlValue();                  
    fvalue.set_url("http://google.com");
    fvalue.set_description("GoogleLink");
oListItem.set_item('URL', fvalue);
oListItem.update();
======================================================

Hope this helps!!!

Friday 10 June 2016

Change the URL of the SharePoint List - Create 2010 Tasks list

Problem:
Sometimes when we create the sharepoint task list with 2013 Tasks template(171). We may not open that task and it results to error to open webparts maintenence page.

Resolution:
To resolve this, create Task list with 107 template id which is available with sharepoint 2010.
To do that, Open designer and create 2010 workflow and create task list with that workflow. It will create 107 template id task list.
But name of the task list will be created with the name Tasks and prefixed with workflow name.
ex: WorkflowNameTasks
To rename, we can open the list in sharepoint and edit the name. But it will not change the url. Url still refers old name, only display name changed now.
To change URL also, open the sharepoint site in explorer view and choose the list and rename. This completes URL change also.
Change the list internal name with the url in sharepoint by opening the site in explorer view and rename the list.

Hope it helps!!!