接上篇。
分析:
1,传入三个参数: listId, 修改全部/修改选中,选中的itemId。
2,修改之前先checkout, 然后记录修改的信息。
实现:
1, 用javascript弹出一个modalDialog窗口:
Code
function GotoEditAuthor(Mode,ListId)
{
var DocumentIds = GetSelectedItems("DocumentID", ",");
var handle = window.showModalDialog('/_layouts/EditAuthorFolder/EditAuthor.aspx?Mode=' + Mode + '&listItems=' + DocumentIds + '&listID=' + ListId, window,'status:off;center=yes;edge:raised;dialogHeight:400px;dialogWidth:450px;help:0;resizable:0;scrollbar=0');
if (handle != null && handle != '' && typeof handle != 'undefined')
{
window.location.href=window.location.href;
}
}
function GotoEditAuthor(Mode,ListId)
{
var DocumentIds = GetSelectedItems("DocumentID", ",");
var handle = window.showModalDialog('/_layouts/EditAuthorFolder/EditAuthor.aspx?Mode=' + Mode + '&listItems=' + DocumentIds + '&listID=' + ListId, window,'status:off;center=yes;edge:raised;dialogHeight:400px;dialogWidth:450px;help:0;resizable:0;scrollbar=0');
if (handle != null && handle != '' && typeof handle != 'undefined')
{
window.location.href=window.location.href;
}
}
2, 修改的代码:
Code
using (mySite = SPContext.Current.Site)
{
using (myWeb = mySite.OpenWeb(strWebName))
{
// 是否存在用户
try
{
DocAuthor = myWeb.AllUsers[TextBoxAuthor.Text];
}
catch
{
DocAuthor = null;
}
if (DocAuthor != null)
{
SPDocumentLibrary myDocLibrary = myWeb.Lists[myDocGuid] as SPDocumentLibrary;
SPListItemCollection myDocItems;
myWeb.AllowUnsafeUpdates = true;
// Mode == 0 is update selected document author;
// Mode == 1 is update all document author;
if (Mode == 0)
{
string[] arrDocID = Regex.Split(strDocItems, ",");
foreach (string myDocID in arrDocID)
{
int DocID;
try
{
DocID = int.Parse(myDocID);
}
catch
{
DocID = 0;
}
if (DocID != 0)
{
SPListItem myItem = myDocLibrary.Items.GetItemById(DocID);
SPFile myFile = myItem.File;
myFile.CheckOut();
myItem["Owner"] = DocAuthor;
myItem.Update();
myFile.CheckIn(System.DateTime.Today.ToShortDateString() + ": Comment: Update document author ");
}
}
}
else if (Mode == 1)
{
myDocItems = myDocLibrary.Items;
foreach (SPListItem myItem in myDocItems)
{
SPFile myFile = myItem.File;
myFile.CheckOut();
myItem["Owner"] = DocAuthor;
myItem.Update();
myFile.CheckIn(System.DateTime.Today.ToShortDateString() + "Comment: Update document author ");
}
}
myWeb.AllowUnsafeUpdates = false;
CloseWindow(true);
}
else
{
LabelMessage.Text = "The User not found! Plese change another one.";
}
}
}
using (mySite = SPContext.Current.Site)
{
using (myWeb = mySite.OpenWeb(strWebName))
{
// 是否存在用户
try
{
DocAuthor = myWeb.AllUsers[TextBoxAuthor.Text];
}
catch
{
DocAuthor = null;
}
if (DocAuthor != null)
{
SPDocumentLibrary myDocLibrary = myWeb.Lists[myDocGuid] as SPDocumentLibrary;
SPListItemCollection myDocItems;
myWeb.AllowUnsafeUpdates = true;
// Mode == 0 is update selected document author;
// Mode == 1 is update all document author;
if (Mode == 0)
{
string[] arrDocID = Regex.Split(strDocItems, ",");
foreach (string myDocID in arrDocID)
{
int DocID;
try
{
DocID = int.Parse(myDocID);
}
catch
{
DocID = 0;
}
if (DocID != 0)
{
SPListItem myItem = myDocLibrary.Items.GetItemById(DocID);
SPFile myFile = myItem.File;
myFile.CheckOut();
myItem["Owner"] = DocAuthor;
myItem.Update();
myFile.CheckIn(System.DateTime.Today.ToShortDateString() + ": Comment: Update document author ");
}
}
}
else if (Mode == 1)
{
myDocItems = myDocLibrary.Items;
foreach (SPListItem myItem in myDocItems)
{
SPFile myFile = myItem.File;
myFile.CheckOut();
myItem["Owner"] = DocAuthor;
myItem.Update();
myFile.CheckIn(System.DateTime.Today.ToShortDateString() + "Comment: Update document author ");
}
}
myWeb.AllowUnsafeUpdates = false;
CloseWindow(true);
}
else
{
LabelMessage.Text = "The User not found! Plese change another one.";
}
}
}
3, 关闭窗口:
Code
private void CloseWindow(bool IsDirty)
{
Response.Write(string.Format("<SCRIPT>top.returnValue = {0}; window.close();<" + "/SCRIPT>", (IsDirty ? "1" : "null")));
Response.End();
}
private void CloseWindow(bool IsDirty)
{
Response.Write(string.Format("<SCRIPT>top.returnValue = {0}; window.close();<" + "/SCRIPT>", (IsDirty ? "1" : "null")));
Response.End();
}
效果:
另: 小弟碰到一个非常奇怪的问题: 我用SPContext.Current.Web的时候, 获取的居然不是当前的web, 非常的困惑,不知有人能告诉我是哪里出了问题?
通常说刚安装好的有 moss , docs , news, reports, search , 然后我的功能是放在docs的, 调试出来的结果一直是moss, 而不是docs?