2

I am using Microsoft Office 365 (Plan P1). I have a user "ABC". I want that whenever ABC logs in, he is redirected to a specific site let's say Marketing site.

I am writing a javascript code as below:

<script type="text/javascript" language="javascript">

if(document.getElementById('<%=LoginName1.ClientID %>') == "ABC"; 
location.href="/teamsite/Home.aspx";

</script>

But I am getting error that code blocks not allowed. Can someone help?


Below is the code of page:

<%-- _lcid="1033" _version="14.0.4762" _dal="1" --%>
<%-- _LocalBinding --%>
<%@ Page language="C#" MasterPageFile="~masterurl/default.master"    Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage,Microsoft.SharePoint,Version=14.0.0.0,Culture=neutral,PublicKeyToken=71e9bce111e9429c" meta:progid="SharePoint.WebPartPage.Document"  %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Import Namespace="Microsoft.SharePoint" %> <%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> <%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitle" runat="server">
<SharePoint:ListItemProperty Property="BaseName" maxlength="40" runat="server"/>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderPageTitleInTitleArea" runat="server">
<WebPartPages:WebPartZone runat="server" title="loc:TitleBar" id="TitleBar" AllowLayoutChange="false" AllowPersonalization="false"><ZoneTemplate>
    <WebPartPages:TitleBarWebPart runat="server" AllowEdit="True" AllowConnect="True" ConnectionID="00000000-0000-0000-0000-000000000000" Title="Web Part Page Title Bar" IsIncluded="True" Dir="Default" IsVisible="True" AllowMinimize="False" ExportControlledProperties="True" ZoneID="TitleBar" ID="g_771de8f3_6bde_473c_ade0_fd62f3baa052" HeaderTitle="Calculating..." AllowClose="False" FrameState="Normal" ExportMode="All" AllowRemove="False" AllowHide="True" SuppressWebPartChrome="False" DetailLink="" ChromeType="None" HelpLink="" MissingAssembly="Cannot import this Web Part." PartImageSmall="" HelpMode="Modeless" FrameType="None" AllowZoneChange="True" PartOrder="1" Description="" PartImageLarge="" IsIncludedFilter="" __MarkupType="vsattributemarkup" __WebPartId="{771DE8F3-6BDE-473C-ADE0-FD62F3BAA052}" WebPart="true" Height="" Width=""></WebPartPages:TitleBarWebPart>

<WebPartPages:TitleBarWebPart runat="server" AllowEdit="True" AllowConnect="True" ConnectionID="00000000-0000-0000-0000-000000000000" Title="Web Part Page Title Bar" IsIncluded="True" Dir="Default" IsVisible="True" AllowMinimize="False" ExportControlledProperties="True" ZoneID="TitleBar" ID="g_ea882083_ab10_4e70_9986_6cbe1640086a" HeaderTitle="Calculating..." AllowClose="False" FrameState="Normal" ExportMode="All" AllowRemove="False" AllowHide="True" SuppressWebPartChrome="False" DetailLink="" ChromeType="None" HelpLink="" MissingAssembly="Cannot import this Web Part." PartImageSmall="" HelpMode="Modeless" FrameType="None" AllowZoneChange="True" PartOrder="2" Description="" PartImageLarge="" IsIncludedFilter="" __MarkupType="vsattributemarkup" __WebPartId="{EA882083-AB10-4E70-9986-6CBE1640086A}" WebPart="true" Height="" Width=""></WebPartPages:TitleBarWebPart>

</ZoneTemplate></WebPartPages:WebPartZone>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderTitleAreaClass" runat="server">
<style type="text/css">
Div.ms-titleareaframe {
height: 100%;
}
.ms-pagetitleareaframe table {
background: none;
}
</style>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">
<meta name="GENERATOR" content="Microsoft SharePoint" />
<meta name="ProgId" content="SharePoint.WebPartPage.Document" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="CollaborationServer" content="SharePoint Team Web Site" />
<script type="text/javascript">
// <![CDATA[
var navBarHelpOverrideKey = "WSSEndUser";
// ]]>
</script>
<SharePoint:UIVersionedContent ID="WebPartPageHideQLStyles" UIVersion="4" runat="server">
    <ContentTemplate>
<style type="text/css">
body #s4-leftpanel {
display:none;
}
.s4-ca {
margin-left:0px;
}
</style>
    </ContentTemplate>
</SharePoint:UIVersionedContent>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderSearchArea" runat="server">
<SharePoint:DelegateControl runat="server"
    ControlId="SmallSearchInputBox"/>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderLeftActions" runat="server">
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderPageDescription" runat="server">
<SharePoint:ProjectProperty Property="Description" runat="server"/>
</asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderBodyRightMargin" runat="server">
<div height="100%" class="ms-pagemargin"><img src="/_layouts/images/blank.gif" width="10" height="1" alt="" /></div>
</asp:Content>

<asp:Content ContentPlaceHolderId="PlaceHolderPageImage" runat="server"></asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderNavSpacer" runat="server"></asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderLeftNavBar" runat="server"></asp:Content>
<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
    <table cellpadding="4" cellspacing="0" border="0" width="100%">
            <tr>
                <td id="_invisibleIfEmpty" name="_invisibleIfEmpty" valign="top" width="100%"> 
                <WebPartPages:WebPartZone runat="server" Title="loc:FullPage" ID="FullPage" FrameType="TitleBarOnly"><ZoneTemplate></ZoneTemplate></WebPartPages:WebPartZone> </td>
            </tr>
            <script type="text/javascript" language="javascript">if(typeof(MSOLayout_MakeInvisibleIfEmpty) == "function") {MSOLayout_MakeInvisibleIfEmpty();}</script>
    </table>
</asp:Content>


<asp:Content>
<script type="text/javascript"> 
ExecuteOrDelayUntilScriptLoaded(getUserData, "sp.js");   
var context = null; 
var web = null; 
var currentUser = null; 
    function getUserData() { 
        context = new SP.ClientContext.get_current(); 
        web = context.get_web(); 
        currentUser = web.get_currentUser(); 
        currentUser.retrieve(); 
        context.load(web); 
        context.executeQueryAsync(Function.createDelegate(this, this.onUserDataSuccess), Function.createDelegate(this, this.onUserDataFailure)); 
    } 
    function onUserDataSuccess(sender, args) { 
        var user = web.get_currentUser(); 
        alert('User name:' + user.get_title() + '\n Login Name:' + userObject.get_loginName()); 
    } 
    function onUserDataFailure(sender, args) { 
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); 
    } 
</script>
</asp:Content>`enter code here`

1 Answer 1

1

You used <% ... %> which is C# code and cannot be used in JavaScript alone.

To get the current user's login name you will need to make use of the SharePoint ECMA script object model. To get the login name you can use the following script:

<script type="text/javascript">
    ExecuteOrDelayUntilScriptLoaded(getUserData, "sp.js");  
    var context = null;
    var web = null;
    var currentUser = null;
        function getUserData() {
            context = new SP.ClientContext.get_current();
            web = context.get_web();
            currentUser = web.get_currentUser();
            currentUser.retrieve();
            context.load(web);
            context.executeQueryAsync(Function.createDelegate(this, this.onUserDataSuccess), Function.createDelegate(this, this.onUserDataFailure));
        }
        function onUserDataSuccess(sender, args) {
            var user = web.get_currentUser();
            alert('User name:' + user.get_title() + '\n Login Name:' + userObject.get_loginName());
        }
        function onUserDataFailure(sender, args) {
            alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
        }
</script>

The important line is web.get_currentUser() which is the current user and afterwards you can do user.get_title(). Exactly in that onUserDataSuccess function you can do your redirect then.

Sign up to request clarification or add additional context in comments.

7 Comments

I have placed this code to test. I am getting the below erorr: Error Only Content controls are allowed directly in a content page that contains Content controls. Troubleshoot issues with Microsoft SharePoint Foundation. Correlation ID: 8ecdacf7-1ffa-4392-8ebc-90cc6fa5d55e Date and Time: 1/9/2012 9:03:57 AM I have tried asp:content tag too
You probably accidently have some markup outside of the <asp:Content controls. Open your page back up and ensure that all code except the <%@ directives are within an <asp:Content block.
I have checked no code is out side the <asp:content></asp:content> block.
Ensure that the <asp:Content> tags are properly capitalized. SharePoint seems to freak out when <asp:content> tag is found. ;-)
I have checked that too - only <asp:Content> is used with a capital 'C'. I have reread the code twice and now pasting it below:
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.