SharePoint 2010/2013 通过List Item的内容菜单(BCD)来拷贝Item
来源:互联网 发布:怎么用网络看电视直播 编辑:程序博客网 时间:2024/06/05 01:03
本文讲述SharePoint 2010/2013 通过List Item的内容菜单(BCD)来拷贝Item的方案。
项目中有时会遇到需要在原有列表项的基础上拷贝列表项进行编辑并保存的的需求,本文将阐述如何实现这一需求。
效果为:
思路是使用List Item的内容菜单(BCD) 和 ashx ,增加一个copy item的菜单(CopyMenuElement):
<?xml version="1.0" encoding="utf-8"?><Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <CustomAction Location="ScriptLink" ScriptSrc="~site/_layouts/15/CopyItem/jquery-1.11.0.min.js" Sequence="99" /> <CustomAction Location="ScriptLink" ScriptSrc="~site/_layouts/15/CopyItem/CopyItem.js" Sequence="100" /> <CustomAction Id="65695319-4784-478e-8dcd-4e541cb1d682.CopyItem" RegistrationType="List" RegistrationId="100" Location="EditControlBlock" Sequence="10001" Title="Copy Item"> <UrlAction Url="javascript:CopyItemById('{ListId}', '{ItemId}','{SiteUrl}')" /> </CustomAction></Elements>
然后再Layout 目录下引入Jquery和定义CopyItem.js:
function CopyItemById(listId, itemId, siteUrl) { var ajaxRequestUrl = siteUrl + "/_layouts/15/CopyItem/CopyItem.ashx?listId=" + listId + "&itemId=" + itemId; $.ajax(ajaxRequestUrl) .done( function (copyResult) { if (!copyResult.ErrorMessage) { var editUrl = GetSiteUrl() + copyResult.UpdateFormUrl + "?ID=" + copyResult.NewItemId + "&source=" + document.location.href; openDialogForMeetingRecord(editUrl, 'Update the copied item'); } else { console.error("Copy Item error:" + copyResult.ErrorMessage); console.error("StackTrace:" + copyResult.ErrorDetail); } } ) .fail(function (error) { errorCallBack(error); });}function openDialogForMeetingRecord(url, title) { var options = { url: url, title: title, dialogReturnValueCallback: CallBackfromDialog }; SP.UI.ModalDialog.showModalDialog(options);}function CallBackfromDialog(dialogResult) { document.location.reload(); /*if (dialogResult == 1) { // TBD:refresh the data } else { // Do nothing }*/}function GetSiteUrl() { var port = ""; if (document.location.port && document.location.port.lenght > 0) { port = ":" + document.location.port; } return document.location.protocol + '//' + document.location.host + port;}在SharePoint 工程中加入 CopyItem.ashx (如何在SharePoint 工程中添加ashx 请参考 http://blog.csdn.net/abrahamcheng/article/details/20490757)
CopyItem.ashx 代码为:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %><%@ Assembly Name="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %><%@ WebHandler Language="C#" Class="CopyItemPorject.CopyItem" %>
CopyItem.ashx.cs 代码为
using System;using Microsoft.SharePoint;using Microsoft.SharePoint.WebControls;using System.Web;namespace CopyItemPorject{ public partial class CopyItem : IHttpHandler { public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); context.Response.ContentType = "application/json"; string jsonResult = string.Empty; try { string listId = context.Request.QueryString["listId"]; string itemId = context.Request.QueryString["itemId"]; SPContext currentContext = SPContext.Current; SPWeb web = SPContext.Current.Web; SPList currentList = web.Lists[new Guid(listId)]; string editFormUrl = currentList.DefaultEditFormUrl; SPListItem newItem = currentList.Items.Add(); SPListItem copyItem = currentList.GetItemById(int.Parse(itemId)); foreach (SPField field in currentList.Fields) { if ((!SPBuiltInFieldId.Contains(field.Id) || field.Id.ToString() == SPBuiltInFieldId.Title.ToString()) && !field.ReadOnlyField) { newItem[field.Id] = copyItem[field.Id]; } } web.AllowUnsafeUpdates = true; newItem.Update(); int newItemId = newItem.ID; jsonResult = jsonSerializer.Serialize(new CopyResult() { NewItemId = newItemId, UpdateFormUrl = editFormUrl }); } catch (Exception ex) { jsonResult = jsonSerializer.Serialize(new CopyResult() { ErrorMessage=ex.Message, ErrorDetail= ex.StackTrace}); } context.Response.Write(jsonResult); } }}
新建一个CopyResult.cs用于存储ashx的返回值:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace CopyItemPorject{ class CopyResult { public string UpdateFormUrl { get; set; } public int NewItemId { get; set; } public string ErrorMessage { get; set; } public string ErrorDetail { get; set; } }}
SharePoint 项目的工程结构为
有需要的朋友可以从这里下载源代码:
https://spcopyitem.codeplex.com/
1 0
- SharePoint 2010/2013 通过List Item的内容菜单(BCD)来拷贝Item
- sharepoint 2010 重置List Item访问权限
- Adding Custom Actions to the List Item Menu in SharePoint 2010 Using SharePoint Designer 2010[Sharepoint中添加自定义菜单]
- sharepoint vs操作site、web、list、item ....
- SharePoint 添加BCD菜单
- SharePoint 添加BCD菜单
- 如何通过右击ListView的Item弹出FlyoutMenu显示的Item的内容。
- combobox怎样根据item内容来决定显示哪一个item
- 通过onTouch来确定点击的是listView哪一个item
- SharePoint 的Discussion List的Discussion Content Type属于Folder 类型,并非是Item类型
- 带图标的list item
- 如何设置SharePoint list,使user只对自己拥有的item进行操作。
- sharepoint:为页面中的list添加简单的item总数显示
- SharePoint 2013 中使用 JavaScript Like 和Unlike list item/page/document
- Item 14 资源管理类的拷贝
- 通过ContextMenu上下文菜单获取ListView中被选中的条目item的信息
- QListWidget的item上实现右键菜单
- QListWidget的item上实现右键菜单
- Pat(Advanced Level)Practice--1029(Median)
- typedef用法小结
- 别做HR最讨厌的求职者
- win32程序启动的过程和启动速度的优化
- poj.1789
- SharePoint 2010/2013 通过List Item的内容菜单(BCD)来拷贝Item
- JavaEE的13种核心技术
- poj2387
- C++类中的变量和构造函数
- tcpdump
- java Struct
- 产品经理总结运营过程中9大心得
- MongoDB的java版本驱动(译文)
- UniGUI的TUniLoginForm窗口自定义背景色