如何从 javascript 设置 SharePoint 应用程序查询字符串令牌

How to set Sharepoint Apps query string tokens from javascript

本文关键字:应用程序 查询 字符串 令牌 SharePoint 设置 javascript      更新时间:2023-09-26

我有以下 asp.net mvc SharePoint 应用程序控制器

  public ActionResult Index(string city)
        {
            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);
            List<Programate> lsProgramate = new List<Programate>();
            ViewBag.Img = spContext.SPHostUrl + "Style Library/Intranet/images/programte-13.png";
            ViewBag.CssRenting = spContext.SPHostUrl + "Style Library/Intranet/css/renting.css";
            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    List lstProducts = clientContext.Web.Lists.GetByTitle("Programate");
                    CamlQuery camlQuery = new CamlQuery();
                    camlQuery.ViewXml = "<View><Query><OrderBy><FieldRef Name='Title'/></OrderBy></Query></View>";
                    //add filter by city
                    ListItemCollection lstEventos = lstProducts.GetItems(camlQuery);
                    clientContext.Load(lstEventos);
                    clientContext.ExecuteQuery();
                    ViewBag.Resp = "";
                    if (lstEventos != null)
                    {
                        foreach (var lstEventoItem in lstEventos)
                        {
                            Programate objProgramate = new Programate();
                            objProgramate.Titulo = lstEventoItem["Title"].ToString();
                            objProgramate.Hora_Inicio = Convert.ToString(lstEventoItem["EventDate"]); 
                            objProgramate.Hora_Fin = Convert.ToString(lstEventoItem["EndDate"]);
                            objProgramate.Descripcion = lstEventoItem["Description"].ToString();
                            FieldLookupValue obj = new FieldLookupValue();
                            obj = (FieldLookupValue)lstEventoItem["Ubicacion"];
                            objProgramate.Ciudad = obj.LookupValue;
                            objProgramate.Ubicacion = lstEventoItem["Location"].ToString();
                            lsProgramate.Add(objProgramate);
                        }
                        List<string> lsStr = lsProgramate.Select(x => x.Ciudad).Distinct().ToList();
                        ViewBag.Ciudades = lsStr;
                    }

                }
            }
            return View(lsProgramate);
        }

在我看来是这样的:

@using xx.Models;
@model List<Programate>
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title></title>  
    <script type="text/javascript" src="~/Scripts/jquery-1.10.2.js"></script>
    <script type="text/javascript" src="~/Scripts/bootstrap.js"></script>
    <link href="~/Content/bootstrap.css" rel="stylesheet" type="text/css" />
    <link href="@ViewBag.CssRenting" rel="stylesheet" type="text/css" />
    <style type="text/css">
        .programate-color {
            width: 95%;
            margin-left: 2.5%;
        }
        .selec-programate {
            width: 95%;
            margin-left: 2.5%;
        }
    </style>
    <script type="text/javascript">
        // Set the style of the app part page to be consistent with the host web.
        // Get the URL of the host web and load the styling of it.
        function setStyleSheet() {
            var hostUrl = ""
            if (document.URL.indexOf("?") != -1) {
                var params = document.URL.split("?")[1].split("&");
                for (var i = 0; i < params.length; i++) {
                    p = decodeURIComponent(params[i]);
                    if (/^SPHostUrl=/i.test(p)) {
                        hostUrl = p.split("=")[1];
                        document.write("<link rel='"stylesheet'" href='"" + hostUrl +
                            "/_layouts/15/defaultcss.ashx'" />");
                        break;
                    }
                }
            }
            // if no host web URL was available, load the default styling
            if (hostUrl == "") {
                document.write("<link rel='"stylesheet'" " +
                    "href='"/_layouts/15/1033/styles/themable/corev15.css'" />");
            }
        }
        setStyleSheet();
    </script>
</head>
<body style="background-color: transparent; overflow: auto;">

    <div id="dynamicContent">
        @Html.DropDownList("Ciudades", new SelectList(ViewBag.Ciudades), new { @class = "form-control selec-programate", @onchange = "CallChangeCiudad(this.value)" });
        <div class="programate-color">
            @{int i = 0;
                foreach (var item in Model)
                {
                    i = i + 1;
                    string collapse = "Evento" + i;
                    <a data-toggle="collapse" data-target="#@collapse">
                        <div class="programte" style="height: 104px; width: 100%;">
                            <img class="img-programte" src="@ViewBag.Img">
                            <p class="fecha-programte">@i</p>
                            <p class="contenido-programte">@item.Titulo</p>
                            <p class="lugar">@item.Ubicacion - @item.Ciudad</p>
                        </div>
                    </a>
                        <div id="@collapse" class="collapse">
                            <div class="row" style="margin-left: 15%;">
                                <div class="col-sm-12">
                                    <p style="color:red; font-size: 18px;">Inicio: @item.Hora_Inicio - Fin: @item.Hora_Fin</p>
                                </div>
                                <div class="col-sm-12">
                                    <p style="font-size:16px; color:#040489;">@item.Descripcion</p>
                                </div>
                            </div>
                        </div>
                }
            }
        </div>
    </div>
    <script>
                function CallChangeCiudad(val) {
                    //window.location.href = "/Programate/Index?ciudad=" + val;
                }
    </script>
    <script type="text/javascript">
    "use strict";
    window.Communica = window.Communica || {};
    $(document).ready(function () {
        Communica.Part.init();
    });
    Communica.Part = {
        senderId: '',
        init: function () {
            var params = document.URL.split("?")[1].split("&");
            for (var i = 0; i < params.length; i = i + 1) {
                var param = params[i].split("=");
                if (param[0].toLowerCase() == "senderid")
                    this.senderId = decodeURIComponent(param[1]);
            }

            this.adjustSize();
        },
        adjustSize: function () {
            var step = 30,
                newHeight,
                contentHeight = $('#dynamicContent').height(),
                resizeMessage = '<message senderId={Sender_ID}>resize({Width}, {Height})</message>';
            newHeight = (step - (contentHeight % step)) + contentHeight;
            resizeMessage = resizeMessage.replace("{Sender_ID}", this.senderId);
            resizeMessage = resizeMessage.replace("{Height}", newHeight);
            resizeMessage = resizeMessage.replace("{Width}", "100%");
            window.parent.postMessage(resizeMessage, "*");
        }
    };
    </script>
</body>
</html>

如您所见,我有一个呈现城市列表的下拉列表,我希望当用户选择一个列表时,它应该使用城市的查询字符串回调控制器索引操作,但这不是问题,问题是 sharepoint 应用程序需要一些查询字符串令牌,以便 sharepoint 可以在操作上创建 clientContext 对象。

如果是服务器端代码,那么它将是这样的:

@Html.ActionLink("Install Design Package", "InstallDesignPackage", "Home", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Current.Request).AbsoluteUri },null)

但是因为这是javascript,我不知道如何在下拉列表更改时附加所有这些标准共享点令牌。

在我们的例子中,这是我们必须手动完成的事情,这是一个示例(请注意,我们依赖于jquery)然后,您所要做的就是将 Utilities.GetSharePointSecurityParams 的结果附加到 ajax 调用的 url 中。

Utilities.GetSharePointSecurityParams = function ()
{
    var re = /'+/g;
    var spAppWebUrl = Utilities.getQueryStringParameterCleaned("SPAppWebUrl");
    var params = {
        SPHostUrl: Utilities.getQueryStringParameterCleaned("SPHostUrl"),
        SPLanguage: Utilities.getQueryStringParameterCleaned("SPLanguage"),
        SPClientTag: Utilities.getQueryStringParameterCleaned("SPClientTag"),
        SPProductNumber: Utilities.getQueryStringParameterCleaned("SPProductNumber"),
        SPHostTitle: Utilities.getQueryStringParameterCleaned("SPHostTitle"),
    };
    if (spAppWebUrl !== "")
        params["SPAppWebUrl"] = spAppWebUrl;
    return $.param(params).replace(re, " ");
}
Utilities.getQueryStringParameter = function (paramToRetrieve) {
    var params = document.URL.split("?")[1].split("&");
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] === paramToRetrieve)
            return singleParam[1];
    }
    return null;
}
Utilities.getQueryStringParameterCleaned = function (paramToRetrieve) {
    var re = /#$/;
    var tempResult = Utilities.getQueryStringParameter(paramToRetrieve);
    if (tempResult != null)
        return decodeURIComponent(tempResult).replace(re, "")
    else
        return "";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>