Tuesday, 12 August 2014

Get SharePoint list of users using powershell commands

First go to server roles -> Click on Features -> Click on add features -> Select windows powershell integrated scripting environment ISE and install it.
Go to Run -> powershell ise
In the last pane(ex: PS....) will come execute below command :
Set-Execution Policy remotesigned
In the first pane copy script as per your requirements and finally copy function calls - if first code executing run first command and mention your URL in http://sp2013 location. 
If it is second script then enter run second  
Attached script will be used to fetch list of all users on SharePoint site and also on any specific list using PowerShell script. Script consits of two functions
GetSPWebUsers
Using this, you can get list of all site users, it will take single parameter (i.e site URL).
PowerShell
function GetSPWebUsers($SiteCollectionURL) 
{ 
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null 
    $site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL) 
    $web = $site.openweb() 
    $siteUsers = $web.SiteUsers 
 
    foreach($user in $siteUsers) 
    {        
        Write-Host " ------------------------------------- " 
        Write-Host "Site Collection URL:"$SiteCollectionURL 
        if($user.IsSiteAdmin -eq $true) 
        { 
            Write-Host "ADMIN: "$user.LoginName 
        } 
        else 
        { 
            Write-Host "USER: "$user.LoginName 
        } 
        Write-Host " ------------------------------------- " 
    }    
    $web.Dispose() 
    $site.Dispose() 
} 
 
GetSPAllSPUsers
This function will return list of all users and their permission set for any list/library. This can be used to check users permissions for against a list. This function have two parameters(i.e. site URL and List Name).

PowerShell
function GetSPAllSPUsers($SiteCollectionURL,$SPListName) 
{ 
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null 
    $site = new-object Microsoft.SharePoint.SPSite($SiteCollectionURL)     
    $web = $site.openweb() 
    $list = $web.Lists[$SPListName] 
    $siteCollUsers = $web.SiteUsers 
     
    foreach($user in $siteCollUsers) 
        { 
            Write-Host " ------------------------------------- " 
            Write-Host "Site Collection URL:"$SiteCollectionURL 
            if($list.DoesUserHavePermissions([Microsoft.SharePoint.SPBasePermissions]::ViewListItems,$user-eq $true) 
                { 
                    Write-Host "User : "$user.LoginName 
                    Write-Host "Assigned Permissions : "$list.GetUserEffectivePermissions($user.LoginName) 
                }             
            Write-Host " ------------------------------------- "         
        } 
     
        $web.Dispose() 
        $site.Dispose() 
 } 
Function Calls
Functins can be called as below

PowerShell
GetSPWebUsers "http://SP2013" 
GetSPAllSPUsers "http://sp2013:1111" "List Name"

Tuesday, 4 March 2014

Http redirect in IIS

The <httpRedirect> element configures settings for Internet Information Services (IIS) 7 that redirect client requests to a new location.
There are several reasons why you might want to redirect clients to a new location. For example, if your company is migrating to a new Web site, you could redirect all requests from the old Web site to the new Web site. Likewise, if you have deployed a new application on a Web server, you could redirect all requests for the old application's URL namespace (for example, http://www.consoto.com/app_v1.0/) to the new applications location (for example, http://www.consoto.com/app_v2.0/).
In the simplest configuration, you need only set the enabled and destination attributes of the <httpRedirect> element in order to redirect clients to a new location. However, additional elements like the exactDestination and httpResponseStatus attributes allow you to configure the end-user experience of the redirection by respectively specifying whether IIS 7 will return the destination URL exactly as entered and which HTTP response code to return to the Web client.

Compatibility

VersionNotes
IIS 7.5The <httpRedirect> element was not modified in IIS 7.5.
IIS 7.0The <httpRedirect> element was introduced in IIS 7.0.
IIS 6.0The <httpRedirect> element replaces the IIS 6.0 HttpRedirect metabase property.

Setup

HTTP Redirection is not available on the default installation of IIS 7. To install it, use the following steps.

WINDOWS SERVER 2008 OR WINDOWS SERVER 2008 R2

  1. On the taskbar, click Start, point to Administrative Tools, and then click Server Manager.
  2. In the Server Manager hierarchy pane, expand Roles, and then click Web Server (IIS).
  3. In the Web Server (IIS) pane, scroll to the Role Services section, and then click Add Role Services.
  4. On the Select Role Services page of the Add Role Services Wizard, expand Common Http Features, select HTTP Redirection, and then click Next.
  5. On the Confirm Installation Selections page, click Install.
  6. On the Results page, click Close.

WINDOWS VISTA OR WINDOWS 7

  1. On the taskbar, click Start, and then click Control Panel.
  2. In Control Panel, click Programs and Features, and then click Turn Windows Features on or off.
  3. Expand Internet Information Services, then World Wide Web Services, then Common Http Features.
  4. Select HTTP Redirection, and then click OK.

How To

There is no user interface for adding wildcard HTTP redirects for IIS 7. For examples of how to add <add> elements to the<httpRedirect> element programmatically, see the Code Samples section of this document.

HOW TO ADD AN HTTP REDIRECT RULE TO A WEB SITE OR APPLICATION

  1. Open Internet Information Services (IIS) Manager:
    • If you are using Windows Server 2008 or Windows Server 2008 R2:
      • On the taskbar, click Start, point to Administrative Tools, and then click Internet Information Services (IIS) Manager.
    • If you are using Windows Vista or Windows 7:
      • On the taskbar, click Start, and then click Control Panel.
      • Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.
  2. In the Connections pane, expand the server name, expand Sites, and then navigate to the Web site or application that you want to configure custom error pages for.
  3. In the Home pane, double-click HTTP Redirect.
  4. In the HTTP Redirect pane, check the box to redirect requests and enter the destination URL.
  5. You can optionally specify any of the following options:
    • Configure the redirection destination to be the exact destination as entered.
    • Configure the redirection destination to be limited to the destination URL's root folder, not subfolders.
    • Configure the HTTP status code, which can be one of these three options:
      • 301 Permanent
      • 302 Found
      • 307 Temporary
      Note: IIS 7 will respectively return the following actual HTTP response statuses for each of the above options:
      • HTTP/1.1 301 Moved Permanently
      • HTTP/1.1 302 Redirect
      • HTTP/1.1 307 Redirect
  6. When you have finished all the above changes, click Apply in the Tasks pane.

Configuration

ATTRIBUTES

AttributeDescription
childOnlyOptional Boolean attribute.

Specifies whether the destination value should be added to the beginning of the file name that contains the request to be redirected. For example, if childOnly were set to true and the destination value were configured to be http://marking.contoso.com/, a request for http://contoso.com/default.htm would be redirected to http://marketing.contoso.com/default.htm.

The default value is false.
destinationOptional string attribute.

Specifies a URL or virtual path to which to redirect the client.
enabledOptional Boolean attribute.

Specifies whether redirection is enabled (true) or disabled (false).

The default value is false.
exactDestinationOptional Boolean attribute.

Specifies that the destination value should be considered an absolute target location, not a relative location.

The default value is false.
httpResponseStatusOptional enum attribute.

Specifies type of redirection.

The httpResponseStatus attribute can be one of the following possible values. The default is Found.
ValueDescription
FoundReturns a 302 status code, which tells the client to issue a new request to the location specified in the destination attribute.

The numeric value is 302.
PermanentReturns a 301 status code, which informs the client that the location for the requested resource has permanently changed.

The numeric value is 301.
TemporaryReturns a 307 status code, which prevents the client from losing data when the browser issues an HTTP POST request.

The numeric value is 307.

CHILD ELEMENTS

ElementDescription
addOptional element.

Adds a wildcard redirection rule to the collection of redirection rules.
clearOptional element.

Removes all references to wildcard redirection rules from the collection of redirection rules.
removeOptional element.

Removes a reference to a wildcard redirection rule from the collection of redirection rules.


Tuesday, 25 February 2014

Redirecting a SharePoint Subsite or a Site collection to a different URL

Lots of times I have been asked this question if there is a way to redirect a subset to a different URL
I have seen many people trying this by playing around AAM and at the IIS level. I will try to put bit of light on both of them and will also show you a easy way out of it
1) AAM(Alternate Access Mapping)
This option will not work out as we cannot add Subsite URL in this. For Example we can add the following types of URL in the AAM
A) http://ServerName
B) http://FQDN
Now this is good if you want to do a redirect of the whole url where if browse to http://ServerNameA we are redirected to http://ServerNameB but this option will not help in redirecting a Subsite to a different URL
2) IIS Level
We can redirect a Virtual Dir with help of this. But SharePoint Subsite wont show up in IIS as they are stored in database. So one way we can think is create a Virtual dir of each URL you want to redirect then create exclusion in SharePoint and redirect it from IIS
Now if you several Subsites then this is a not a good option.
Following are the steps which will help us in redirecting any level of site in SharePoint in a pretty neat manner. We will try to redirect http://mosssps/sites/Team/default.aspx to http://mosssps
1) We will open the site in Web Folder view
clip_image002
2) Download default.aspx page of the site which we want to redirect. There are other ways to access default.aspx page too.
clip_image004
3) Open the default.aspx in notepad and delete all the contents of it
4) Add the following code to it
<%@ Page language="C#" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=12.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" %>
<script>
window.location.href = "http://mosssps/Pages/Default.aspx";
</script>
5) http://mosssps/Pages/Default.aspx is the URL where we want it to redirect if browse to http://mosssps/sites/Team/default.aspx
6) Upload the Default.aspx and now when you browse to http://mosssps/sites/Team/default.aspx it will redirect tohttp://mosssps/Pages/Default.aspx.

Note- I have only tested this on Moss.If you are intending to use this for V2 then you may wanna test this first