[C#.NET][Sharepoint 2013] 封存檔案 - Enable In Place Records on Document Library
使用者情境:
- 當文件進入封存後,無法修改該檔案的任何屬性,包含內容,但能夠下載檔案
PS.CSOM 目前不支援 Records,所以在這裡使用的是 Server-Site Object Model
本文章節
啟用 『In Place Records Management』
如下步驟:
只有頂層網站有『In Place Records Management』,如下圖:
子網站若需要該功能,得自行啟用
開啟 Record,如下圖:
如下步驟:
成功之後,會看到圖示有一把鎖
重新整理畫面,鎖的圖示就會不見。
使用 Server API Declare Record & Undeclare Record
準備環境
- Install Sharepoint Foundation 2013
- Add Reference Microsoft.SharePoint.dll & Microsoft.Office.Policy
- Server-Side API:http://msdn.microsoft.com/en-us/library/cc768559.aspx
- 啟用 『In Place Records Management』
開發重點
- Declare Record:調用 Microsoft.Office.RecordsManagement.RecordsRepository.RecordRecords.DeclareItemAsRecord(SPListItem)
- Undeclare Record:調用 Microsoft.Office.RecordsManagement.RecordsRepository.RecordRecords.Records.UndeclareItemAsRecord(SPListItem)
- 調用 DeclareItemAsRecord(SPListItem) 或 UndeclareItemAsRecord(SPListItem) 時,必需要將整個 SPSite 釋放,才會讓第二次的命令生效
完整範例如下:
using Microsoft.Office.RecordsManagement.RecordsRepository; using Microsoft.SharePoint; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplication1 { internal class Program { private const string s_TopSite = @"http://sps2013"; private const string s_SubSite = @"/csomapi"; private const string s_List = @"Documents"; private const string s_FileName = @"myDoc.docx"; private static void Main(string[] args) { addFile(); recordFile(); unrecordFile(); } private static void addFile() { using (SPSite site = new SPSite(s_TopSite)) { using (SPWeb web = site.OpenWeb(s_SubSite)) { SPFolder list = web.Lists[s_List].RootFolder; FileStream fileStream = File.OpenRead(s_FileName); SPFile file = list.Files.Add(s_FileName, fileStream, true); file.Update(); } } Console.WriteLine("addFile 按任意鍵繼續"); Console.Read(); } private static void recordFile() { using (SPSite site = new SPSite(s_TopSite)) { using (SPWeb web = site.OpenWeb(s_SubSite)) { var list = web.Lists[s_List].RootFolder; var files = list.Files; foreach (SPFile file in files) { var item = file.Item; Console.WriteLine(item.Name); if (item.Name == s_FileName) { Console.WriteLine("IsRecord: " + Records.IsRecord(item)); Console.WriteLine("IsLocked: " + Records.IsLocked(item)); if (!Records.IsRecord(item)) { Records.DeclareItemAsRecord(item); file.Update(); Console.WriteLine("Record Finish"); } break; } } } } Console.WriteLine("addFile 按任意鍵繼續"); Console.ReadKey(); } private static void unrecordFile() { using (SPSite site = new SPSite(s_TopSite)) { using (SPWeb web = site.OpenWeb(s_SubSite)) { var list = web.Lists[s_List].RootFolder; var files = list.Files; foreach (SPFile file in files) { var item = file.Item; Console.WriteLine(item.Name); if (item.Name == s_FileName) { Console.WriteLine("IsRecord: " + Records.IsRecord(item)); Console.WriteLine("IsLocked: " + Records.IsLocked(item)); if (Records.IsRecord(item)) { Records.UndeclareItemAsRecord(item); file.Update(); Console.WriteLine("Unrecord Finish"); } break; } } } } Console.WriteLine("unrecordFile 按任意鍵繼續"); Console.ReadKey(); } } }
本文出自:http://www.dotblogs.com.tw/yc421206/archive/2014/06/09/145460.aspx
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET