Check current User is a member of specific Group – SharePoint 2013

Today, I ran in to a requirement restriction only for specific page in site page library.

Blow Script help that how to check the Current user in specific group.

http://site/SiteAssets/jquery-3.1.0.min.js

function IsCurrentUserMemberOfGroup(groupName, OnComplete) {

var currentContext = new SP.ClientContext.get_current();
var currentWeb = currentContext.get_web();

var currentUser = currentContext.get_web().get_currentUser();
currentContext.load(currentUser);

var allGroups = currentWeb.get_siteGroups();
currentContext.load(allGroups);

var group = allGroups.getByName(groupName);
currentContext.load(group);

var groupUsers = group.get_users();
currentContext.load(groupUsers);

currentContext.executeQueryAsync(OnSuccess,OnFailure);

function OnSuccess(sender, args) {
var userInGroup = false;
var groupUserEnumerator = groupUsers.getEnumerator();
while (groupUserEnumerator.moveNext()) {
var groupUser = groupUserEnumerator.get_current();
if (groupUser.get_id() == currentUser.get_id()) {
userInGroup = true;
break;
}
}
OnComplete(userInGroup);
}

function OnFailure(sender, args) {
OnComplete(false);
}

}

function ValidatePerviledges(){

IsCurrentUserMemberOfGroup(“ISO_Approvers”, function (isCurrentUserInGroup) {
if(isCurrentUserInGroup)
{
console.log(“Access granted”);

} else{

//Redirect to Access denied page

window.location.replace(“http://site/DocumentControl/_layouts/15/accessdenied.aspx”)
}

});

}

$(document).ready(function(){

//Wait for SP.js

ExecuteOrDelayUntilScriptLoaded(ValidatePerviledges, “sp.js”);

});

 

Enable Session State in your web application

You’d need to enable Session State in your web application.

  1. Execute the following powershell script Enable-SPSessionStateService –DefaultProvision

  2. Apply the following web.config change:

    pages enableSessionState="true"

Get Data From SharePoint Pictures Library Through REST API Using JSOM

In Document ready function prepare the code to access the SharePoint Picture library.this example shows how to filter and order by with REST API.

According to substringof method this request(get) will retrieve the image names(FileLeafRef) contains ”s190x90″ and ordered by ‘Image name(FileLeafRef).

Loading…

http://~/site/jquery-3.1.0.min.js

$(document).ready(function()
{

$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + “/_api/web/lists/getbytitle(‘TrainingCompletionStatus’)/items”,
type: “GET”,
headers: {
“accept”: “application/json;odata=verbose”,
},
success: function (data)
{

console.log(data.d.results);
$.each( data.d.results, function(index,item)
{
console.log(item.Title+”-“+item.Completion);
///Here Title and Completion are my propties of JSON object that received from SP List
});
},
error: function (error) {
alert(JSON.stringify(error));
}
});

}) ;

 

Remove Sub sites with Powershell

Removing subsides is easier from powershell;

Use below script to remove sites

Below I am reading site urls  from a text file

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function Remove-ChildSites([Microsoft.SharePoint.SPWeb]$Web)
{
Foreach($ChildWeb in $Web.Webs)
{
#Call the function recursively TO DELETE all sub-childs
Remove-ChildSites($ChildWeb)
}
Write-host Removing web $Web.Url
#Remove the web
Remove-SPWeb $Web -Confirm:$false
}

foreach($line in Get-Content C:\Users\TEMP\Documents\sites.txt) {
if($line -match $regex)
{
write-host $line
$ParentWebURL=$line

#Get the Parent Web
$ParentWeb= Get-SPWeb $ParentWebURL

#Call the function to remove all child webs
Remove-ChildSites $ParentWeb

}
}

 

Subtract DateTime in sql

I was struggling to subtract    days and   hours from a Datetime field

Below code helped me to do it in one sec.

declare @createTime datetime = ‘2012-10-06 02:29:37.243’;
select @createtime as originaltime, dateadd(day, -4, dateadd(hour,-1,@createtime))as minus4dayandhour

 

Empty your database

Below command will make your database empy

USE master
IF EXISTS(select * from sys.databases where name='yourDBname')
DROP DATABASE yourDBname

CREATE DATABASE yourDBname

Disable Form while uploading file.

Paste below Script on View

$(document).on(‘invalid-form.validate’, ‘form’, function () {
var button = $(this).find(‘input[type=”submit”]’);
setTimeout(function () {
button.removeAttr(‘disabled’);
}, 1);
});
$(document).on(‘submit’, ‘form’, function () {
var button = $(this).find(‘input[type=”submit”]’);
setTimeout(function () {
button.attr(‘disabled’, ‘disabled’);
}, 0);
});

Send Email with Branded Images

Hi guys,

Here Im going to give you a code sample for sending branded emails with c# .

private void SendAccountConfirmEmailB(string to)
{

int UserId = GetUserIdByEmail(to);
/

// string body = “Thank you for creating an account to submit your poster or storyboard. Please” + “<a href=’ihi-2016-PosterPresentation/Home/ActivateAccount/” + UserId + “”> click here</a> order to active your account and log in”;
string from = “noreply@masterbadge.com”;
try
{
string body = “<html><body><img src=\”cid:Logo\”><br><strong>Hello,</strong></p>” +
“<br>Thank you for creating an account to submit your poster or storyboard. Please <a href=’ihi-2016-PosterPresentation/Home/ActivateAccount/” + UserId + “”> click here</a> order to active your account and log in.</body></html>”;

string path = Server.MapPath(“~/Images/MLogo.jpg”);
LinkedResource logo = new LinkedResource(path, MediaTypeNames.Image.Jpeg);
logo.ContentId = “Logo”;
AlternateView av1 = AlternateView.CreateAlternateViewFromString(body, null, System.Net.Mime.MediaTypeNames.Text.Html);
av1.LinkedResources.Add(logo);

// string body = “Thank you for creating an account to submit your poster or storyboard. Please” + “<a href=’ihi-2016-PosterPresentation/Home/ActivateAccount/” + UserId + “”> click here</a> order to active your account and log in”;

MailMessage msg = new MailMessage(from, to, ” Forum 2016 – Account Activation “, body);
SmtpClient emailClient = new SmtpClient(“smtp.office365.com”, 587);
System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(“noreply@sssxxx.com”, “xxxxxxxx11”);
msg.IsBodyHtml = true;
msg.AlternateViews.Add(av1);
emailClient.UseDefaultCredentials = false;
emailClient.Credentials = SMTPUserInfo;
emailClient.EnableSsl = true;
emailClient.Send(msg);

}
catch (Exception ex)
{
}
}

Simple Slider with Next And Previous

Hi Coders, today i got a task to do a slider in to sharepoint app so i had to design one  slider by my self (pure jquery only).i would like to share my code with someone who is looking for slider.

Script Code

https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js

$(document).ready(function () {

var currentBox=0;

$(“.NavRight”).click(function () {

var tobox = $(“.mainbox”).length;
if(currentBox

 

HTML code

Second Qatar National Obstetric Ultrasound Training Course

</div>

Internal Medicine Board Review Course

</div>
</div>

Css code

<style>
body {
background-image: url(“http://marhaba.qa/wp-content/uploads/2012/12/photo-qncc-busy-with-delegates.jpg?w=300&#8221;);
}
.sliderOutbx {
width: 816px;
display: block;
padding: 20px;
height: 370px;
background-color: rgba(255,255,255,.7);
}

.NavRight {
background-image: url(“Images/R-Arrow.png”);
float: right;
height: 10em;
background-repeat: no-repeat;
margin-top: 3em;
width:25px;
cursor:pointer;

}
.NavLeft {
background-image: url(“Images/L-Arrrow.png”);
float: left;
height: 10em;
background-repeat: no-repeat;
margin-top: 3em;
width: 25px;
margin-right: 4.2em;
cursor: pointer;
}

.box {

width: 289px;
height: 247px;
background-color: rgba(0,121,193,.8);
float: left;
margin-right: 3em;
position: relative;
}
.readmorebox {
text-align:center;
height:46px;
position:absolute;
bottom:0px;
width:inherit;
background-color:whitesmoke;
}

.mainbox {
display:inline-block;
}
.titlebx {
color: white;
font-family: sans-serif;
position: absolute;
left: 5%;
top: 16%;
}

.ReadmoreLink {
font-family: sans-serif;
text-decoration: none;
color: rgb(0,121,193);
}

</style>