Search This Blog

Tuesday 19 November 2013

Get User Profile Information in SharePoint 2013



To retrieve user profile information in SharePoint 2013, we need to add below references in out SharePoint Project.

           1.       Microsoft.Office.Server
           2.       Microsoft.Office.Server.UserProfiles

Refer below code snippets for reference

Code behind :
private void GetUserProfileInfo()
        {
            try
            {
                UserProfileManager usrProfileMgr = new UserProfileManager(SPServiceContext.GetContext(SPContext.Current.Site));
                UserProfile usrProfile = usrProfileMgr.GetUserProfile(SPContext.Current.Web.CurrentUser.LoginName);
                if (usrProfile != null)
                {
                    lblName.Text = usrProfile.DisplayName;
                    string origUrl = (string)usrProfile[PropertyConstants.PictureUrl].Value;
                    if (!string.IsNullOrEmpty(origUrl))
                    {
                        imgUser.ImageUrl = origUrl;
                    }
                    else
                    {
                        imgUser.ImageUrl = "/_layouts/15/images/PersonPlaceholder.96x96x32.png";
                    }
                    lblDesignation.Text = Convert.ToString(usrProfile[PropertyConstants.JobTitle].Value);
                    lblDepartment.Text = Convert.ToString(usrProfile[PropertyConstants.Department].Value);
                    lblEmail.Text = Convert.ToString(usrProfile[PropertyConstants.WorkEmail].Value);
                }
            }
            catch (Exception ex)
            {
                lblError.Text = ex.Message;
            }
        }
Here, GetUserProfileInfo() method we have called in Page_Load
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetUserProfileInfo();
            }
        }
 
Ascx 

<table cellpadding="5px" cellspacing="0">
    <tr>
        <td>
            <asp:Image ID="imgUser" runat="server" Height="50px" Width="50px" />
        </td>
        <td>
            <asp:Label ID="lblName" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblDesignation" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblDepartment" runat="server"></asp:Label>
            <br />
            <asp:Label ID="lblEmail" runat="server"></asp:Label>
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <asp:Label ID="lblError" runat="server"></asp:Label>
        </td>
    </tr>
</table>

Result:

More Reference:
Link 1
Link 2
Link 3

Monday 18 November 2013

Sign in as Different User in SharePoint 2013

One of features used in testing of permissions in SharePoint is "Sign in as Different User" which allows you to log in as another user.  With SharePoint 2013 this option is missing.

To get this feature back follow the below steps :

    Locate and then open the following file in a text editor:  C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\CONTROLTEMPLATES\Welcome.ascx

    Add the following element before the existing "ID_RequestAccess" element:

    <SharePoint:MenuItemTemplate runat="server" ID="ID_LoginAsDifferentUser" Text="<%$Resources:wss,personalactions_loginasdifferentuser%>" Description="<%$Resources:wss,personalactions_loginasdifferentuserdescription%>" MenuGroupId="100" Sequence="100" UseShortId="true" />

    Save the file.

Microsoft has published a KB article on the same : http://support.microsoft.com/kb/2752600.

Hope this information was helpful.

Source : Link 1

SharePoint 2013 classic mode authentication

Woo..... Started with SP 2013 Posts

As a first step I have started to create new site, I just move to SP 2013 Central Administration to create new Web Application. But in SP 2013 Claims Authentication is the default one. So we need PowerShell scripts for Classic Mode Authentication. Refer the below script to create new web application in Classic Mode Authentiacation.

Reference: http://technet.microsoft.com/en-us/library/gg276326.aspx

Web Application : http://MyCustomSite:5050

New-SPWebApplication -Name "MyCustomSite" -ApplicationPool "MyCustomSiteApplPool" -AuthenticationMethod "NTLM" -ApplicationPoolAccount (Get-SPManagedAccount "HTS\farmadmin") -Port 5050 -URL "http://MyCustomSite"

Note:     If you do not mention Port and URL, it will take dynamic port number and domain name as URL.
              URL - It is the Public url for this web application

After this we need to create Top level site collection to access site, we can do that using CA or PowerShell, Refer belowscript to create Top level site collection using "Teamsite" template.

New-SPSite -Url "http://MyCustomSite:5050" -OwnerAlias "HTS\rmanikandan" -Template "STS#0" (STS#0 - Teamsite)

Reference: http://technet.microsoft.com/en-us/library/cc263094.aspx

Monday 19 August 2013

SharePoint Lists Column Validation


In our day to day development life, we often come across validating business rules & some common validation related to Email, Phone numbers & Zip Code. In SharePoint foundation 2010, we can achieve validation using formula under Column Validation section. We will check in this post on how to validate Email, Phone numbers & Zip Code using SharePoint out of Box Column Validation formulas.
1.        Email Column Validation
Open list and go to List settings
Click on Email column.
Insert below formula for validating email
Formula:
=AND(
    ISERROR(FIND(" ", [Email],1)),
    IF(ISERROR(FIND("@", [Email],2)),
        FALSE,
        AND(
            ISERROR(FIND("@",[Email], FIND("@", [Email],2)+1)),
            IF(ISERROR(FIND(".", [Email], FIND("@", [Email],2)+2)),
                FALSE,
                FIND(".", [Email], FIND("@", [Email],2)+2) < LEN([Email])
            )
        )
    )
)

Enter [Email] = [Enter your SharePoint Column Name]
The above formula will automatically make this a required column since the validation doesn’t allow blank columns. An easy fix for this is to wrap the above formula in an OR statement with an ISBLANK function. So something like this:
=OR(ISBLANK([YourColumnName]), And Formula From Above)

2.       Phone Number Column Validation
Open list and go to List settings.
Click on Phone Number Column.
Insert below formulas into Column Validation
Formula
=AND(
    LEN([Phone])=14,
    IF(ISERROR(FIND("(", [Phone],1)),
        FALSE,
        (FIND("(", [Phone]) = 1)
    ),
    IF(ISERROR(FIND(")", [Phone],5)),
        FALSE,
        (FIND(")", [Phone], 5) = 5)
    ),
    IF(ISERROR(FIND(" ", [Phone],6)),
        FALSE,
        (FIND(" ", [Phone], 6) = 6)
    ),
    IF(ISERROR(FIND("-", [Phone],10)),
        FALSE,
        (FIND("-", [Phone], 10) = 10)
    ),
    IF(ISERROR(1*CONCATENATE(MID([Phone], 2, 3), MID([Phone], 7, 3), MID([Phone], 11, 4))),
        FALSE,
        AND(
            1*CONCATENATE(MID([Phone], 2, 3), MID([Phone], 7, 3), MID([Phone], 11, 4)) > 1000000000,
            1*MID([Phone], 2, 3) <> 911,
            1*MID([Phone], 7, 3) <> 911,
            1*MID([Phone], 7, 3) <> 555
        )
    )
)
Here [Phone] = [Enter your column name]
The above formula will automatically make this a required column since the validation doesn’t allow blank columns. An easy fix for this is to wrap the above formula in an OR statement with an ISBLANK function. So something like this: =OR(ISBLANK([YourColumnName]), And Formula From Above)

3.       Zip/Postal Code Validation

Open list and go to List settings.
Click on Zip/Postal Code Column.
Insert below formula into Column Validation
Formula

=OR([ZIP/Postal Code]="",LEN([ZIP/Postal Code])=5,
AND(EXACT(UPPER([ZIP/Postal Code]),LOWER([ZIP/Postal Code])),
LEN([ZIP/Postal Code])=10,NOT(ISERROR(FIND("-",[ZIP/Postal Code],6)))))
Here [Zip/Postal code] = [Enter your column name]
The above formula will automatically make this a required column since the validation doesn’t allow blank columns. An easy fix for this is to wrap the above formula in an OR statement with an ISBLANK function. So something like this: =OR (ISBLANK([YourColumnName]), And Formula From Above)

 References