Monthly Archives: April 2015

Enable STS Session Cookie & LogonTokenCacheExpirationWindow in SharePoint

Enable STS Session Cookie

$sts = Get-SPSecurityTokenServiceConfig
$sts.UseSessionCookies = $true
$sts.Update()

LogonTokenCacheExpirationWindow for the SharePoint STS is 10 minutes. The relying party by default it sets the token lifetime in ADFS to be 2 minutes.

LogonTokenCacheExpirationWindow in SharePoint

$sts = Get-SPSecurityTokenServiceConfig
$sts.LogonTokenCacheExpirationWindow = (New-TimeSpan minutes 1)
$sts.Update()

Then do an IISRESET

Advertisements

Add a Custom Actions on Site Settings Menu – SharePoint

If you want to add a custom Action on Site Settings menu, you just need follow the steps bellow.

On your solution, add an Empty Element -> (Add > New Item > Empty Element)

When you add an Empty Element, a xml file was created too, you just need copy-past the following code to Elements.xml file and save:

(green letters represents what you need to change)

Code Example:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomActionGroup
  Id="MyConfigurationGroup"
  Location="Microsoft.SharePoint.SiteSettings"
  Title="Menu of Actions - FcSharePoint" 
  Sequence="10"
  ImageUrl="/_layouts/Images/STSICON.GIF"/>

<CustomAction
  Id="MyConfigPage1"
  GroupId="MyConfigurationGroup"
  Location="Microsoft.SharePoint.SiteSettings"
  Title="Action FcSharePoint"    
  Rights="MangeWeb"
  ImageUrl="/_layouts/Images/STSICON.GIF">
<UrlAction Url="~site/_layouts/Pages/Test.aspx"/> 
</CustomAction>
</Elements>

CustomAction Element

<CustomAction
  ContentTypeId = "Text"
  ControlAssembly = "Text"
  ControlClass = "Text"
  ControlSrc = "Text"
  Description = "Text"
  GroupId = "Text"
  Id = "Text"
  ImageUrl = "Text"
  Location = "Text"
  RegistrationId = "Text"
  RegistrationType = "Text"
  RequireSiteAdministrator = "TRUE" | "FALSE"
  Rights = "Text"
  Sequence = "Integer"
  ShowInLists = "TRUE" | "FALSE"
  ShowInReadOnlyContentTypes = "TRUE" | "FALSE"
  ShowInSealedContentTypes = "TRUE" | "FALSE"
  Title = "Text">
</CustomAction>

Build and Send email method to SharePoint – Using Outgoing SMTP Configuration on Central Administration.

To create and Send emails on sharepoint programmatically you can use the following code, but first you need configure the following things:

  • Configure SMTP on server.
  • Configure Outgoing E-Mail on Central Admininstration

(Central Administration > System Settings > Configure outgoing e-mail settings)

  • Oubound SMTP server is server’s IP.

Code:

StringBuilder body = new StringBuilder();
body.Append("TEXT OF BODY EMAIL"); // TextBox
string subjectEmail = "SUBJECT OF EMAIL"; // TextBox
string senderTo = "fcsharepoint@outlook.com"; //TextBox

SendEmail(senderTo, subjectEmail, body);

Method:

 public bool SendEmail(string sTo, string sSubject, StringBuilder sBody)
 {
  // Email header
  StringBuilder sHeader = new StringBuilder();
  sHeader.Append("<html>");
  sHeader.Append("<body>");

  // Email footer
  StringBuilder sFooter = new StringBuilder();
  sFooter.Append("</body>");
  sFooter.Append("</html>");

  string htmlBody = sHeader.ToString() + sBody.ToString() + sFooter.ToString();

   //Get the Sharepoint SMTP information from the SPAdministrationWebApplication
  string smtpServer = 
  SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
   
  string smtpFrom = 
  SPAdministrationWebApplication.Local.OutboundMailSenderAddress;

 // Add the alternate views instead of using MailMessage.Body
  MailMessage m = new MailMessage();
  m.From = new MailAddress(smtpFrom);
  m.To.Add(new MailAddress(sTo));
  m.Subject = sSubject.ToString();
  m.Body = htmlBody;
  m.IsBodyHtml = true;

 SmtpClient client = new SmtpClient(smtpServer);

 try
 {
   client.Send(m);
   return true;
 }
  catch (Exception) { }
  return false;
}

Add users to SharePoint Group using PowerShell

You can Add user on SharePoint’s Group using Powershell, to do that you just need to know two things:

  • User domain name -> domain\User
  • Group’s name – you can check this on the following post – http://wp.me/p4Hadm-45

Then you can run the following powershell script:

$site=get-spsite http://fcsharepoint

$web=$site.RootWeb

$web.SiteGroups |select name

$user=$web.EnsureUser("LOCALDOMAIN\FCShareUser")

$group=$web.SiteGroups["FCSharepoint Members"]

$group.AddUser($user)

Check if Current User is member of SharePoint Group’s – Programmatically c#

In SharePoint, we have the 3 predetermined permission groups:

  • Visitors
  • Members
  • Owners

You can see this permissions grpups in the /_layouts/permsetup.aspx page.

(Site settings->People and Groups->Settings->Setup groups)

There are properties on the SPWeb class:

  • SPWeb.AssociatedVisitorGroup
  • SPWeb.AssociatedMemberGroup
  • SPWeb.AssociatedOwnerGroup

Programmatically you can check if current user are in one of those Groups, using the following code:

bool isVisitor = false;
bool isMember = false;
bool isOwner = false;

SPWeb web = SPControl.GetContextWeb(System.Web.HttpContext.Current);
            
SPContext currentContext = SPContext.Current;
                  
if (currentContext != null && currentContext.Web.CurrentUser != null)
{
   SPGroupCollection webGroups = web.CurrentUser.Groups;
   if (webGroups != null && webGroups.Count > 0)
   {
       foreach (SPGroup group in web.Groups)
       {
         if (group.ToString() == web.AssociatedVisitorGroup.ToString())
         {
            isVisitor = web.IsCurrentUserMemberOfGroup(group.ID);
         }
         if (group.ToString() == web.AssociatedMemberGroup.ToString())
         {
            isMember = web.IsCurrentUserMemberOfGroup(group.ID);
         }
          if (group.ToString() == web.AssociatedOwnerGroup.ToString())
         {
            isOwner = web.IsCurrentUserMemberOfGroup(group.ID);
         }
       }
   }
if(isVisitor == true)
{
 // User is Visitor
}
if(isMember == true)
{
  // User is Member
}
if(isOwner == true)
{
  // User is Owner
}

List all Groups and all Users SharePoint – PowerShell

The scrit below list all Groups and all Users at sitecollection of SharePoint.

$site = Get-SPSite http://fcsharepoint

$groups = $site.RootWeb.sitegroups

foreach ($grp in $groups)
{
    "Group: " + $grp.name;
     foreach ($user in $grp.users)
     {
         " User: " + $user.name
     }
}
$site.Dispose()

SharePoint Foundation 2013 – Apply MasterPage on Site and SubSites – PowerShell Script

On SharePoint Foundation 2013 you can’t easily apply MasterPage on Subsites and Site because the Feature Site Publishing are not available.

So, if you what to do that, you can use powershell, using the script bellow:

SubSites: 

$site = Get-SPSite http://fcsharepoint:1000
$CustomMasterpage = "_catalogs/masterpage/CUSTOM.master"
foreach ($web in $site.AllWebs)
{
   $web.MasterUrl = $site.RootWeb.ServerRelativeUrl + $CustomMasterpage;
   $web.Update();
   $web.Dispose();
}

Hide Ribbon and SuitBar of SharePoint 2013 for a specifique Zone – Using JavaScript and C#

One way to hide Ribbon and SuitBar of SharePoint 2013 for a specific zone, like Internet for example – you can use Javascript + Css and run it in a UserControl.

1 – Create a simple UserControl on your solution and add it on your MasterPage.

In ASCX file of your UserControl add the following code:

<script type="text/javascript">
    function hideRibbon(){
        document.getElementById("s4-ribbonrow").style.display = "none !important";
        document.getElementById("suiteBar").style.display = "none !important";
    }
</script>

On code behind of UserControl,  you go execute the javascript function, using the following code:

Example to hide in Internet zone

using Microsoft.SharePoint.Administration;
...

protected void Page_Load(object sender, EventArgs e)
{
 if (SPContext.Current.Site.Zone == SPUrlZone.Internet)
 {
 Page.ClientScript.RegisterStartupScript(this.GetType(),"","hideRibbon();",true);
 }
}

Listing / Installing / Uninstalling / Enabling / Disabling Features on Farm, Site Collection and Site – Sharepoint Powershell

Listing Features

Show all the features on the farm:

 Get-SPFeature | Sort -Property DisplayName

Show all the features on the Farm grouped by scope in a table use:

 Get-SPFeature | Sort -Property DisplayName, Scope | FT -GroupBy Scope DisplayName

Show all features for a Web Application:

 Get-SPFeature -WebApplication http://webapplication

Show all features for a Site Collection:

 Get-SPFeature -Site http://sitecollection

Show all features for a Site:

 Get-SPFeature -Web http://siteurl

Enabling and Disabling Features

To disable and enable features:

Disable-SPFeature

 Disable-SPFeature -Identity "NameOfFeature" -URL http://url

Enable-SPFeature

 Enable-SPFeature -Identity "NameOfFeature" -URL http://url

Installing and Uninstalling Features

To install and uninstalling a feature you need to specify the name of the folder that your feature contains:

Install-SPFeature
 Install-SPFeature "FeatureFolderName"
Uninstall-SPFeature
Uninstall-SPFeature "FeatureFolderName"