本文講述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/