本文讲述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>
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" %>
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); } } }
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; } } }
有需要的朋友可以从这里下载源代码:
https://spcopyitem.codeplex.com/
SharePoint 2010/2013 通过List Item的内容菜单(BCD)来拷贝Item,布布扣,bubuko.com
SharePoint 2010/2013 通过List Item的内容菜单(BCD)来拷贝Item
原文:http://blog.csdn.net/abrahamcheng/article/details/20831579