通过sharepoint aspx页面中的java脚本获取用户名

时间:2012-01-09 15:36:30

标签: sharepoint

我正在使用Microsoft Office 365(计划P1)。我有一个用户“ABC”。我希望每当ABC登录时,他都会被重定向到一个特定的网站,让我们说营销网站。

我正在编写如下的javascript代码:

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

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

</script>

但我收到的错误是代码阻止不允许。有人可以帮忙吗?


以下是页面代码:

<%-- _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 个答案:

答案 0 :(得分:1)

您使用的是<% ... %>,这是C#代码,不能仅在JavaScript中使用。

要获取当前用户的登录名,您需要使用SharePoint ECMA脚本对象模型。要获取登录名,您可以使用以下脚本:

<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>

重要的一行是web.get_currentUser(),这是当前用户,然后你可以user.get_title()。正好在onUserDataSuccess函数中,您可以进行重定向。