SharePoint 2010/2013 通过List Item的内容菜单(BCD)来拷贝Item

本文讲述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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值