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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 华为畅享7plus卡机怎么办 华为畅享8plus图标字小怎么办 华为畅享6反应慢发热怎么办 华为畅享5S反应迟钝怎么办 华为畅玩5x玩王者荣耀卡怎么办 不小心把手机里的照片删了怎么办 u盘文件在手机上删了怎么办 荒野行动透视挂功能加载失败怎么办 白色t桖衫被奶茶弄脏了该怎么办 游戏文件不小心解压到c盘了怎么办 装系统时c盘0mb怎么办 电脑设置的开机密码忘了怎么办 电脑开机密码忘了怎么办xp系统 我的电脑在开机时忘了密码怎么办? xp桌面我的电脑图标不见了怎么办 游戏全屏时卡了无法退到界面怎么办 u盘插电脑上提示有病毒怎么办 三星手机文件怎么删除不掉怎么办 用夜神模拟器玩第五人格太卡怎么办 雷电模拟器玩刺激战场太卡了怎么办 绝地求生刺激战场模拟器太卡怎么办 ddj sb2打碟功能没了怎么办 驼背怎么办 要能快速矫正的方法 苹果7中间的home键坏了怎么办 苹果6p的home键不管用怎么办 华为获取数据失败请检查网络怎么办 三星手机未解锁刷机变砖怎么办 手机显示充电但是充不进去怎么办 手机拔出显示无法连接移动网怎么办 手机上的音乐老是显示网络忙怎么办 华为手机账号换手机忘记密码怎么办 墨墨背单词的注册邮箱忘了怎么办 华为手机华为账号密码忘记了怎么办 手机玩游戏降频特别厉害怎么办 苹果应用商店id登录老卡怎么办 苹果id忘记了自动续费怎么办 红米手机小米账号密码忘了怎么办 小米5splus没系统卡米怎么办 注册谷歌账号输入手机好怎么办 谷歌注册电话号码用了太多次怎么办 内存卡用了深度清理的软件怎么办