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 获得当前用户- this.context = new SP.ClientContext.get_current();
- this.web = this.context.get_web();
- this.currentUser = this.web.get_currentUser();
- this.currentUser.retrieve();
- var _spUserId= _spPageContextInfo.userId
- currentcontext.load(currentweb);
- 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 |
- <script type="text/ecmascript" language="ecmascript">
- var fieldCollection;
- var oneField = null;
- function runcode() {
- var clientContext = SP.ClientContext.get_current();
- if (clientContext != undefined && clientContext != null) {
- var webSite = clientContext.get_web();
- taskList = webSite.get_lists().getByTitle("Tasks");
- fieldCollection = taskList.get_fields();
- this.oneField = fieldCollection.getByInternalNameOrTitle("Title");
- this.oneField.set_description("MyNewFieldDescription");
- this.oneField.update();
- clientContext.load(this.fieldCollection);
- clientContext.load(this.oneField);
- clientContext.executeQueryAsync(Function.createDelegate(this, this.OnLoadSuccess), Function.createDelegate(this, this.OnLoadFailed));
- }
- }
- function onl oadSuccess(sender, args) {
- var fieldInfo = '';
- fieldInfo += 'Field Title: ' + oneField.get_title() + '\n' + 'Description: ' + oneField.get_description() + '\n';
- alert(fieldInfo);
- }
- function onl oadFailed(sender, args) {
- alert('Request Failed. ' + args.get_message() + '\n' + args.get_stackTrace());
- }
- </script>
- <input id="Button1" type="button" value="Run Code" οnclick="runcode()" />
通过 REST 使用列表
以下示例演示如何创建列表。
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 标题后删除最后一个字。
- var s2=tText.substr(0,tText.length-1);
- 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()
{
//……比如计算某值
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。