首页 > Web开发 > 详细

转:SharePoint 2013 使用JQuery 一些记录如(SharePoint js 获得当前用户)等基本操作。

时间:2021-02-15 23:43:41      阅读:34      评论:0      收藏:0      [点我收藏+]

SharePoint 2013 使用JQuery 一些记录。

REST API 引用和示例

https://msdn.microsoft.com/zh-cn/library/jj860569.aspx
 
 
Rest API
https://msdn.microsoft.com/zh-cn/magazine/dn198245.aspx
 
 
sharepoint Js 获得当前用户
 
  1.  
            this.context = new SP.ClientContext.get_current();
  2.  
            this.web = this.context.get_web();
  3.  
            this.currentUser = this.web.get_currentUser();
  4.  
            this.currentUser.retrieve();
  5.  
            var _spUserId= _spPageContextInfo.userId
  6.  
            currentcontext.load(currentweb);
  7.  
            var login = user.get_loginName();


 
 

下面是服务器端对象模型(Microsoft.SharePoint.dll)与JS对象模型的比较图:

服务器端对象模型

JS对象模型

Microsoft.SharePoint.SPContext

SP.ClientContext

Microsoft.SharePoint.SPSite

SP.Site

Microsoft.SharePoint.SPWeb

SP.Web

Microsoft.SharePoint.SPList

SP.List

Microsoft.SharePoint.SPListItem

SP.ListItem

Microsoft.SharePoint.SPField

SP.Field

 
技术分享图片获得一个值然后填充
参加地址:https://msdn.microsoft.com/en-us/library/office/jj246815.aspx
  1.  
    <script type="text/ecmascript" language="ecmascript">
  2.  
     
  3.  
    var fieldCollection;
  4.  
    var oneField = null;
  5.  
    function runCode() {
  6.  
    var clientContext = SP.ClientContext.get_current();
  7.  
    if (clientContext != undefined && clientContext != null) {
  8.  
    var webSite = clientContext.get_web();
  9.  
    taskList = webSite.get_lists().getByTitle("Tasks");
  10.  
     
  11.  
    fieldCollection = taskList.get_fields();
  12.  
    this.oneField = fieldCollection.getByInternalNameOrTitle("Title");
  13.  
     
  14.  
    this.oneField.set_description("MyNewFieldDescription");
  15.  
    this.oneField.update();
  16.  
     
  17.  
    clientContext.load(this.fieldCollection);
  18.  
    clientContext.load(this.oneField);
  19.  
    clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
  20.  
    }
  21.  
    }
  22.  
     
  23.  
    function OnLoadSuccess(sender, args) {
  24.  
    var fieldInfo = ‘‘;
  25.  
    fieldInfo += ‘Field Title: ‘ + oneField.get_title() + ‘\n‘ + ‘Description: ‘ + oneField.get_description() + ‘\n‘;
  26.  
    alert(fieldInfo);
  27.  
    }
  28.  
     
  29.  
    function OnLoadFailed(sender, args) {
  30.  
    alert(‘Request failed. ‘ + args.get_message() + ‘\n‘ + args.get_stackTrace());
  31.  
    }
  32.  
    </script>
  33.  
     
  34.  
    <input id="Button1" type="button" value="Run Code" οnclick="runCode()" />


以下示例演示如何创建列表。

 
url: http://site url/_api/web/lists
method: POST
body: { ‘__metadata‘: { ‘type‘: ‘SP.List‘ }, ‘AllowContentTypes‘: true, ‘BaseTemplate‘: 100,
 ‘ContentTypesEnabled‘: true, ‘Description‘: ‘My list description‘, ‘Title‘: ‘Test‘ }
Headers: 
    Authorization: "Bearer " + accessToken
    X-RequestDigest: form digest value
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body

以下示例演示如何使用 MERGE 方法更新列表。

 
url: http://site url/_api/web/lists(guid‘list GUID‘)
method: POST
body: { ‘__metadata‘: { ‘type‘: ‘SP.List‘ }, ‘Title‘: ‘New title‘ }
Headers: 
    Authorization: "Bearer " + accessToken
    X-RequestDigest: form digest value
    IF-MATCH": etag or "*"
    X-HTTP-Method: MERGE,
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body

以下示例演示如何为列表创建自定义字段。

 
Url: url: http://site url/_api/web/lists(guid‘list GUID‘)/Fields
Method:POST
Body: { ‘__metadata‘: { ‘type‘: ‘SP.Field‘ }, ‘Title‘: ‘field title‘, ‘FieldTypeKind‘: FieldType value,‘Required‘: ‘true/false‘, ‘EnforceUniqueValues‘: ‘true/false‘,‘StaticName‘: ‘field name‘}
Headers: 
    Authorization: "Bearer " + accessToken
    X-RequestDigest: form digest value
    content-type: "application/json;odata=verbose"
    content-length:length of post body

以下示例演示如何删除列表。

 
url: http://site url/_api/web/lists(guid‘list GUID‘)
method: POST
Headers: 
    Authorization: "Bearer " + accessToken
    X-RequestDigest: form digest value
    IF-MATCH: etag or "*"
    X-HTTP-Method: DELETE

技术分享图片 通过 REST 使用列表项

以下示例显示如何"检索"列表的所有项。

 
url: http://site url/_api/web/lists/GetByTitle(‘Test‘)/items
method: GET
headers:
    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose" or "application/atom+xml"

以下示例显示如何"检索"特定列表项。

 
url: http://site url/_api/web/lists/GetByTitle(‘Test‘)/items(item id)
method: GET
headers:
    Authorization: "Bearer " + accessToken
    accept: "application/json;odata=verbose" or "application/atom+xml"

以下 XML 显示当您请求 XML 内容类型时返回的列表项属性的示例。

 
<content type="application/xml">
<m:properties> 
<d:FileSystemObjectType m:type="Edm.Int32">0</d:FileSystemObjectType>
<d:Id m:type="Edm.Int32">1</d:Id>
<d:ID m:type="Edm.Int32">1</d:ID>
<d:ContentTypeId>0x010049564F321A0F0543BA8C6303316C8C0F</d:ContentTypeId>
<d:Title>an item</d:Title>
<d:Modified m:type="Edm.DateTime">2012-07-24T22:47:26Z</d:Modified>
<d:Created m:type="Edm.DateTime">2012-07-24T22:47:26Z</d:Created>
<d:AuthorId m:type="Edm.Int32">11</d:AuthorId>
<d:EditorId m:type="Edm.Int32">11</d:EditorId>
<d:OData__UIVersionString>1.0</d:OData__UIVersionString>
<d:Attachments m:type="Edm.Boolean">false</d:Attachments>
<d:GUID m:type="Edm.Guid">eb6850c5-9a30-4636-b282-234eda8b1057</d:GUID>
</m:properties>
</content>

以下示例显示如何"创建"列表项。

 
 
url: http://site url/_api/web/lists/GetByTitle(‘Test‘)/items
method: POST
body: { ‘__metadata‘: { ‘type‘: ‘SP.Data.TestListItem‘ }, ‘Title‘: ‘Test‘}
headers:
    Authorization: "Bearer " + accessToken
     X-RequestDigest: form digest value
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body

以下示例显示如何"更新"列表项。

 
 
url: http://site url/_api/web/lists/GetByTitle(‘Test‘)/items(item id)
method: POST
body: { ‘__metadata‘: { ‘type‘: ‘SP.Data.TestListItem‘ }, ‘Title‘: ‘TestUpdated‘}
headers:
    Authorization: "Bearer " + accessToken
     X-RequestDigest: form digest value
    "IF-MATCH": etag or "*"
    "X-HTTP-Method":"MERGE",
    accept: "application/json;odata=verbose"
    content-type: "application/json;odata=verbose"
    content-length:length of post body

以下示例显示如何"删除"列表项。

 
url: http://site url/_api/web/lists/GetByTitle(‘Test‘)/items(item id)
method: POST
headers:
    Authorization: "Bearer " + accessToken
     X-RequestDigest: form digest value
    "IF-MATCH": etag or "*"
    "X-HTTP-Method":"DELETE"
1,读取SharePoint list 标题后删除最后一个字。
  1.  
    var s2=tText.substr(0,tText.length-1);
  2.  
    alert(s2);

效果
技术分享图片
技术分享图片


JS URL 解码

 
 //解码
         var s9=("http://portal.com.cn/_layouts/15/AD/adPicker.aspx?ou=%E9%BB%84%E6%B2%B3%E6%B0%B4%E7%94%B5&IsDlg=1");
          alert(decodeURI(s9));
          //弹出来的是解码后的原始结果。
 

前后台代码互相调用

C#前台js调用后台代码
前台js
<script type="text/javascript" language="javascript">
  function Ceshi()
  {
    var a = "<%=Getstr()%>";
    alert(a);
  }
</script>
<input type="button" οnclick="Ceshi();" value="js调用后台代码" />
后台代码
public string Getstr()
{
  string aa = "你们好啊!";
  return aa;
}

C#后台调用前台js代码
前台js
<script type="text/javascript" language="javascript">
  function Ceshi()
  {
    var a = "你们好啊!"
    alert(a);
  }
</script>
<asp:Button ID="Button1" runat="server" Text="后台调用js" οnclick="Button1_Click" />
后台代码
protected void Button1_Click(object sender, EventArgs e)
    {
    //如果有UpdatePanel就用如下代码调用前台js
    ScriptManager.RegisterStartupScript(UpdatePanel1, this.Page.GetType(), "", "Ceshi();", true);
    //如果没有就如下代码
    this.Page.ClientScript.RegisterStartupScript(this.Page.GetType(), "", "<script>Ceshi();</script>", true);
    }

javascript函数中执行C#代码中的函数:
方法一:1、首先建立一个按钮,在后台将调用或处理的内容写入button_click中;
        2、在前台写一个js函数,内容为document.getElementByIdx("btn1").click();--母版页:  document.getElementById("<%=txtName.ClientID%>");
        3、在前台或后台调用js函数,激发click事件,等于访问后台c#函数;

方法二:1、函数声明为public             
            后台代码(把public改成protected也可以)
           public string ss()
            {
              return("a");
            }
        2、在html里用<%=fucntion()%>可以调用
            前台脚本
           <script language=javascript>
            var a = "<%=ss()%>";
            alert(a);
           </script>
方法三:1、<script language="javascript">
           <!- -
            function __doPostBack(eventTarget, eventArgument)
            {
               var theForm = document.Form1;     //指runat=server的form
               theForm.__EVENTTARGET.value = eventTarget;
               theFrom.__EVENTARGUMENT.value = eventArgument;
               theForm.submit();
            }
           -->
           </script>
           <input id="Button1" type="button" name="Button1" value="按钮" οnclick="javascript:__doPostBack(‘Button1‘,‘‘)">
        
方法四:<script language="javascript">
function SubmitKeyClick()
{
    if (event.keyCode ==13)
     {
        event.cancelBubble = true;
        event.returnValue = false;
         document.all.FunName.value="你要调用的函数名";
         document.form[0].submit();
     }
}
</script>

<INPUT οnkeypress="SubmitKeyClick()" id="aaa" type="text">
<input type="hidden" name="FunName"> 〈!--用来存储你要调用的函数 --〉

在.CS里有:
public Page_OnLoad()
{
if (!Page.IsPost())
{
string strFunName=Request.Form["FunName"]!=null?Request.Form["FunName"]:"";
//根据传回来的值决定调用哪个函数
switch(strFunName)
{
case "enter()":
enter() ; //调用该函数
break;
case "其他":
//调用其他函数
break;
default:
//调用默认函数
break;
}
}
}

public void enter()
{
//……比如计算某值
}

转:SharePoint 2013 使用JQuery 一些记录如(SharePoint js 获得当前用户)等基本操作。

原文:https://www.cnblogs.com/hanrend/p/14403981.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!