设为首页 收藏本站
查看: 1167|回复: 0

[经验分享] sharepoint 2010 记录管理 对象模型

[复制链接]

尚未签到

发表于 2015-9-27 07:07:55 | 显示全部楼层 |阅读模式
  首先说一下什么是记录管理:这里有详细的说明
  在 网站设置-》网站集管理-》网站集功能 中启用 “现场记录管理”
DSC0000.png
  启用现场记录管理后在 网站管理 中多了2个功能“内容管理器设置” 和“内容管理器规则”
DSC0001.png
  选择一个列表的库设置-》记录声明设置:
DSC0002.png
  然后再文档-》项目中会出现 申明记录
DSC0003.png
  声明为记录后 默认是不能修改和删除, 如果要取消声明 也需要相应的代码
  声明为记录后:
DSC0004.png
  如果我们直接删除会有 声明后果了:
DSC0005.png
DSC0006.png
  其实 文档的修改也有类似的情况。为什么会这样了?
  让我们定位到 网站设置-》网站集管理-》记录声明设置: DSC0007.png
  1)声明记录和取消声明
  声明记录方法: Records.DeclareItemAsRecord(item)
  取消声明记录: Records.UndeclareItemAsRecord(item)
  这里需要引用一些相关的DLL:
  Microsoft.Office.DocumentManagement
  Microsoft.Office.Policy
  主要代码如下:



private static void RecordTest()
{
using (SPSite site = new SPSite(siturl))
{
SPWeb web = site.RootWeb;
SPList list = web.GetList(SharePointListURL);
SPFolder folder = web.Folders[SharePointListURL];
Stream fileStream = File.Open(filePath, FileMode.Open);
SPFile file = list.RootFolder.Files.Add(fileSharePointURL, fileStream);
SPListItem item = file.Item;
file.Update();
Console.WriteLine("In Place Records enabled: " + Records.IsInPlaceRecordsEnabled(site).ToString());
//Declare the item as a record
                Records.DeclareItemAsRecord(item);
//Make sure it declared
object dateObject = item[Expiration.ExpirationDateFieldInternalName];
if (dateObject == null)
{
Console.WriteLine("Not declared!");
}
else
{
DateTime date = (DateTime)dateObject;
Console.WriteLine("Declared Expiration Date: " + date.ToShortDateString() + " " + date.ToShortTimeString());
//Also show if Record using IsRecord
Console.WriteLine("IsRecord: " + Records.IsRecord(item));
//Could also use OnHold to check if on hold
                }
//Undeclare the object
                Records.UndeclareItemAsRecord(item);
web.Close();
}
}
  2)创建保留计划
  如果想在列表上创建一个保留策略,首先要检查列表手否有自定义策略,这个需要Microsoft.Office.RecordsManagement.InformationPolicy.ListPolicySettings对象的ListHasPolicy属性,,返回值表示列表是否具有自定义策略。要想将列表设置为使用一个自定义策略,只需要将UseListPolicy设置为true,然后调用update方法。主要代码如下:



  private static void PolicyList()
{
using (SPSite site = new SPSite(siturl))
{
SPWeb web = site.RootWeb;
SPList list = web.GetList(SharePointListURL);
SPFolder folder = web.Folders[SharePointListURL];
SPWeb parentWeb = list.ParentWeb;
SPList parentList = parentWeb.Lists[folder.ParentListId];
ListPolicySettings listPolicySettings = new ListPolicySettings(parentList);
string policyXml = @"
<Schedules nextStageId='4' default='false'>
<Schedule type='Default'>
<stages>
<data stageId='1' recur='True' offset='6' unit='months'>
<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>
<number>6</number>
<property>Created</property>
<period>months</period>
</formula>
<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.DeletePreviousVersions' />
</data>
<data stageId='2'>
<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>
<number>6</number>
<property>Modified</property>
<period>months</period>
</formula>
<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.Record' />
</data>
</stages>
</Schedule>
<Schedule type='Record'>
<stages>
<data stageId='3'>
<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>
<number>3</number>
<property>Created</property>
<period>years</period>
</formula>
<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.Delete' />
</data>
</stages>
</Schedule>
</Schedules>
";
if (!listPolicySettings.UseListPolicy)
{
//Enable Location Based Policy if it isn't enabled
listPolicySettings.UseListPolicy = true;
listPolicySettings.Update();
//Refresh to get the updated ListPolicySettings
listPolicySettings = new ListPolicySettings(parentList);
}
  listPolicySettings.SetRetentionSchedule(folder.ServerRelativeUrl, policyXml, "My Custom Retention");
listPolicySettings.Update();
Console.WriteLine(listPolicySettings.GetRetentionSchedule(folder.ServerRelativeUrl));
web.Close();
}
}
  这里的保留策略是与列表绑定的,一搬建议与内容类型绑定。



  private static void PolicyContentType()
{
using (SPSite site = new SPSite(siturl))
{
SPWeb web = site.RootWeb;
SPList list = web.GetList(SharePointListURL);
SPFolder folder = web.Folders[SharePointListURL];
SPWeb parentWeb = list.ParentWeb;
SPList parentList = parentWeb.Lists[folder.ParentListId];
ListPolicySettings listPolicySettings = new ListPolicySettings(parentList);
string policyXml = @"
<Schedules nextStageId='4' default='false'>
<Schedule type='Default'>
<stages>
<data stageId='1' recur='True' offset='6' unit='months'>
<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>
<number>6</number>
<property>Created</property>
<period>months</period>
</formula>
<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.DeletePreviousVersions' />
</data>
<data stageId='2'>
<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>
<number>6</number>
<property>Modified</property>
<period>months</period>
</formula>
<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.Record' />
</data>
</stages>
</Schedule>
<Schedule type='Record'>
<stages>
<data stageId='3'>
<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>
<number>3</number>
<property>Created</property>
<period>years</period>
</formula>
<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.Delete' />
</data>
</stages>
</Schedule>
</Schedules>
";
SPContentType contentType = web.ContentTypes["文档"];
Policy policy = Policy.GetPolicy(contentType);
//Check to see if it exists, if not create it
if (policy == null)
{
Policy.CreatePolicy(contentType, null);
policy = Policy.GetPolicy(contentType);
}
PolicyItem retentionPolicy = policy.Items[Expiration.PolicyId];
//See if a policy already exists, if not create one
if (retentionPolicy == null)
{
policy.Items.Add(Expiration.PolicyId, policyXml);
policy.Update();
}
else
{
retentionPolicy.CustomData = policyXml;
retentionPolicy.Update();
}
//Return back policy XML to make sure it worked
retentionPolicy = policy.Items[Expiration.PolicyId];
Console.WriteLine("Policy XML: " + retentionPolicy.CustomData.ToString());
web.Close();
}
}
  运行后的结果如下:
  首先定位到“网站内容类型” -》文档-》信息管理策略设置:
DSC0008.png
  3)创建组织器规则
  必须使用Microsoft.Office.RecordsManagement.RecordsRepository.EcmDocumentRoutingWeb对象。必须在站点功能设置中激活内容组织器功能.
  网站操作->管理网站功能:
DSC0009.png
  如果想基于一个唯一的属性实现自动折叠,那么可以使用DocumentRouterAutoFolderSettings类,主要代码如下:



   public static void PolicyTest()
{
using (SPSite site = new SPSite(siturl))
{
SPWeb web = site.RootWeb;
SPList list = web.GetList(SharePointListURL);
SPFolder folder = web.Folders[SharePointListURL];
SPWeb parentWeb = list.ParentWeb;
EcmDocumentRoutingWeb router = new EcmDocumentRoutingWeb(web);
foreach (EcmDocumentRouterRule rule in router.RoutingRuleCollection)
{
string s = "Alias:" + rule.Aliases + " AFP:" + rule.AutoFolderPropertyName + " Cond:" + rule.ConditionsString + " CTS:"
+ rule.ContentTypeString + " CR:" + rule.CustomRouter + " PRI:" + rule.Priority + " TP:" + rule.TargetPath
+ " Name:" + rule.Name + " Desc:" + rule.Description;
DocumentRouterAutoFolderSettings autoFolder = rule.AutoFolderSettings;
s += "name Format: " + autoFolder.AutoFolderFolderNameFormat
+ " PropID:" + autoFolder.AutoFolderPropertyId.ToString()
+ " InternalName:" + autoFolder.AutoFolderPropertyInternalName
+ " PropName:" + autoFolder.AutoFolderPropertyName
+ " TypeasString:" + autoFolder.AutoFolderPropertyTypeAsString
+ " MaxItem:" + autoFolder.MaxFolderItems.ToString()
+ " Term:" + autoFolder.TaxTermStoreId.ToString();
Console.WriteLine(s);
}
SPContentType contentType = web.ContentTypes["问题"];
string contentString = contentType.Id.ToString() + "|" + contentType.Name;
SPField fieldname = contentType.Fields["标题"];
string fieldNamestring = fieldname.Id.ToString() + "|" + fieldname.InternalName + "|" + fieldname.Title;
  EcmDocumentRouterRule newRule = new EcmDocumentRouterRule(web);
newRule.Name = "Custom Category Rule";
newRule.Description = "Created by Gavin";
newRule.Priority = "5";
newRule.ContentTypeString = contentString;
newRule.TargetPath = "/SiteCollectionDocuments";
newRule.ConditionsString = @"<Conditions>
<Condition Column='8553196d-ec8d-4564-9861-3dbe931050c8|FileLeafRef|Name' Operator='IsNotEqual' Value='NotEqualTo' />
<Condition Column='8553196d-ec8d-4564-9861-3dbe931050c8|FileLeafRef|Name' Operator='GreaterThan' Value='GreaterTha=' />
<Condition Column='8553196d-ec8d-4564-9861-3dbe931050c8|FileLeafRef|Name' Operator='LessThan' Value='LessThan' />
<Condition Column='8553196d-ec8d-4564-9861-3dbe931050c8|FileLeafRef|Name' Operator='GreaterThanOrEqual' Value='GreaterThanEqual' />
<Condition Column='8553196d-ec8d-4564-9861-3dbe931050c8|FileLeafRef|Name' Operator='LessThanOrEqual' Value='LessThanOrEqua=' />
<Condition Column='8553196d-ec8d-4564-9861-3dbe931050c8|FileLeafRef|Name' Operator='BeginsWith' Value='BeginsWith' />
</Conditions>";
SPField customField = contentType.Fields["说明"];
DocumentRouterAutoFolderSettings afaoler = newRule.AutoFolderSettings;
afaoler.Enabled = true;
afaoler.AutoFolderPropertyInternalName = customField.InternalName;
afaoler.AutoFolderPropertyId = customField.Id;
afaoler.AutoFolderPropertyName = customField.Title;
afaoler.AutoFolderPropertyTypeAsString = customField.TypeAsString;
afaoler.AutoFolderFolderNameFormat = "%1-%2";
   newRule.Enabled = true;
router.RoutingRuleCollection.Add(newRule);
}
}
  运行结果如图:
  网站管理 ->内容管理器规则
DSC00010.png
DSC00011.png
  不知道为什么这里用“问题”内容内型后规则不能编辑,之间用自定义的内容内型是没有问题的。
  正过代码需要几个常量定义
  public const string siturl="http://center.beauty.com/";
  private const string SharePointListURL = "http://center.beauty.com/Documents/";
private const string filePath = @"c:\demo.docx";
private const string fileSharePointURL = "http://center.beauty.com/Documents/demo.docx";
  这里也附加一段 删除自定义内容内型的代码:


DSC00012.gif


public static void RemoveContentType()
{
using (SPSite siteCollection = new SPSite(siturl))
{
using (SPWeb webSite = siteCollection.OpenWeb())
{
// Get the obsolete content type.
SPContentType obsolete = webSite.ContentTypes["CustomDC"];
// We have a content type.
if (obsolete != null)
{
IList<SPContentTypeUsage> usages = SPContentTypeUsage.GetUsages(obsolete);
// It is in use.
if (usages.Count > 0)
{
Console.WriteLine("The content type is in use in the following locations:");
foreach (SPContentTypeUsage usage in usages)
Console.WriteLine(usage.Url);
}
// The content type is not in use.
else
{
// Delete it.
Console.WriteLine("Deleting content type {0}...", obsolete.Name);
webSite.ContentTypes.Delete(obsolete.Id);
}
}
// No content type found.
else
{
Console.WriteLine("The content type does not exist in this site collection.");
}
}
}
Console.Write("\nPress ENTER to continue...");
Console.ReadLine();
}
View Code   

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-119222-1-1.html 上篇帖子: SharePoint 2010 技巧系列 下篇帖子: SharePoint 2010 技巧系列: 文档管理的自动分发功能
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表