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

[经验分享] 如何通过编程方式将SharePoint中的Infopath表单及附件进行归档

[复制链接]
累计签到:6 天
连续签到:1 天
发表于 2015-9-24 10:40:40 | 显示全部楼层 |阅读模式
  事情是这样的:我们通过Infopath设计了一些电子表单,用来在企业内部进行一些流程审批的载体。结合SharePoint Server所提供的Forms Service,我们可以比较便捷地实现,在线填写也很方便,如下图所示
DSC0000.png
  在浏览器中填写的效果如下
DSC0001.png
  注意:这只是一个演示表单,我简单做了几个字段而已。重点要体现附件。
DSC0002.png
  
  那么,现在的需求是这样:因为这些表单越来越多,而且大多有附件,导致SharePoint的内容数据库越来越大,速度受到一定的影响。用户想到一个做法,就是定期将那些已经完成审批的表单归档,而且从这个表单库中删除掉。
  在归档的时候,就会遇到一个问题,如何将附件也归档,并且放到指定的磁盘文件夹上去
  
  首先,我们需要了解的是,Infopath的附件默认是怎么存储的呢?Infopath表单其实就是一份特殊的XML文件,它会将所有的信息,包括附件在内都保存在一个XML文件中。当然,附件会通过编码成Base64的字符串保存。我们可以将之前填写好的这个表单保存下来一份数据,以便了解它里面的结构
DSC0003.png
  这个文件,我们可以用记事本直接打开
DSC0004.png
  大家可以看到,其实附件的内容都是保存在这个xml文件里面的。
  通过一些研究,我实现了如下的解决方案,这是一个原型,可以作为一个参考。
  【备注】具体在做的时候,还要细致一些将所有数据都妥善保存,本例重点演示如何保存附件。其他常规的数据应该很容易处理。可以读取出来存放在数据库中的一个表中。
  
  下面这个类型是我在网上找到的,不是我的原创。这个类型是一个解码器,可以将上面的Base64String还原为一个字节数组
  using System;
using System.Collections.Generic;
using System.Text;

using System.IO;

/// <summary>
/// Class used to decode an InfoPath attachment.
/// Pulls the file name and the decoded file from either a base 64 byte array or string.
/// </summary>
public class InfoPathAttachmentDecoder
{
// Private string to hold the attachment name.
string _fileName;

// Private byte array to hold the decoded attachment.
byte[] _decodedFile;

/// <summary>
/// The name of the file within the InfoPath attachment.
/// </summary>
public string Filename
{
get { return _fileName; }
}

/// <summary>
/// The decoded file within the InfoPath attachment.
/// </summary>
public byte[] DecodedFile
{
get { return _decodedFile; }
}

/// <summary>
/// Constructor for the InfoPathAttachmentDecoder Class
/// </summary>
/// <param name="base64EncodedString">The attachment represented by a string</param>
public InfoPathAttachmentDecoder(string base64EncodedString)
{
// Use unicode encoding.
Encoding _encoding = Encoding.Unicode;

// The byte array containing the data.
byte[] _data = Convert.FromBase64String(base64EncodedString);

// Use a memory stream to access the data.
using(MemoryStream _memoryStream = new MemoryStream(_data))
{
// Create a binary reader from the stream.
BinaryReader _theReader = new BinaryReader(_memoryStream);

// Create a byte array to hold the header data.
byte[] _headerData = _theReader.ReadBytes(16);

// Find the file size before finding the file name.
int _fileSize = (int)_theReader.ReadUInt32();

// Get the file name.
int _attachmentNameLength = (int)_theReader.ReadUInt32() * 2;
byte[] _fileNameBytes = _theReader.ReadBytes(_attachmentNameLength);
_fileName = _encoding.GetString(_fileNameBytes, 0, _attachmentNameLength - 2);

// Get the decoded attachment.
_decodedFile = _theReader.ReadBytes(_fileSize);
}
}

/// <summary>
/// Constructor for the InfoPathAttachmentDecoder Class
/// </summary>
/// <param name="base64EncodedBytes">The attachment represented by a byte array</param>
public InfoPathAttachmentDecoder(byte[] base64EncodedBytes) : this(Convert.ToBase64String(base64EncodedBytes)) { }

/// <summary>
/// Static method that gets the file from the attachment.
/// </summary>
/// <param name="base64EncodedString">The attachment represented by a string</param>
/// <returns>Returns a byte array of the file in the attachment.</returns>
public static byte[] DecodeInfoPathAttachment(string base64EncodedString)
{
// Create an instance of the InfoPathAttachmentDecoder
InfoPathAttachmentDecoder _infoPathAttachmentDecoder = new InfoPathAttachmentDecoder(base64EncodedString);

// Return the decoded file.
return _infoPathAttachmentDecoder.DecodedFile;
}

/// <summary>
/// Static method that gets the file from the attachment.
/// </summary>
/// <param name="base64EncodedBytes">The attachment represented by a byte array</param>
/// <returns>Returns a byte array of the file in the attachment.</returns>
public static byte[] DecodeInfoPathAttachment(byte[] base64EncodedBytes)
{
// Create an instance of the InfoPathAttachmentDecoder
InfoPathAttachmentDecoder _infoPathAttachmentDecoder = new InfoPathAttachmentDecoder(base64EncodedBytes);

// Return the decoded file.
return _infoPathAttachmentDecoder.DecodedFile;
}
}
  
  然后,我们需要实现对XML文档的读取。虽然读XML文件向来都不是什么大问题,但Infopath的XML文档结构还是挺繁琐的,有很多命名空间,直接读取相当费时费力。我一般会用下面的方式
  
  1. 打开Visual Studio Command Prompt
  2. 根据xml文件生成xsd(架构)
DSC0005.png
  3.根据xsd文件生成一个强类型的class
DSC0006.png
  
  准备工作做好了,下面我们做一个简单的程序来实现一下整个存档的逻辑

1. 创建一个Windows Forms程序
DSC0007.png
  【注意】选择.NET Framework 3.5
  

2. 设置编译平台为x64(这是访问SharePoint服务器对象模型的要求)
DSC0008.png
  

3. 做一个简单的界面如下
DSC0009.png
  

4. 引用Microsoft.SharePoint.dll
DSC00010.png
  

5. 将之前生成好的10248.cs和写好的InfoPathAttachmentDecoder 类型添加到项目中来
DSC00011.png
  

6.编写代码
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
using Microsoft.SharePoint;
namespace FormArchiver
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void btStart_Click(object sender, EventArgs e)
{
var path = txtLibPath.Text;
var folder = txtFolder.Text;
var site = new SPSite(path);
var web = site.OpenWeb();
var list = web.GetList(path);
var items = list.Items;
foreach(SPListItem item in items)
{
var file = item.File;
var stream = file.OpenBinaryStream();
var serializer = new XmlSerializer(typeof(myFields));//这里的myFields这个类型,是之前通过xsd工具根据表单结构生成的
var result =(myFields) serializer.Deserialize(stream);
var attachment = new InfoPathAttachmentDecoder(result.group3.FirstOrDefault().field4);
var fileName = attachment.Filename;
var buffer = attachment.DecodedFile;
if(!Directory.Exists(folder))
Directory.CreateDirectory(folder);
var targetPath = Path.Combine(folder, fileName);
File.WriteAllBytes(targetPath, buffer);
}
MessageBox.Show("保存完成");
}
}
}

  

  这个程序运行起来的效果大致如下
DSC00012.png
  
  点击“开始”,很快的我们就可以将附件保存出来到预设的目录
DSC00013.png
  
  这个演示程序的源代码,请通过这里下载
  FormArchiver.rar

运维网声明 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-118107-1-1.html 上篇帖子: SharePoint 2013备份方法整理 下篇帖子: Sharepoint学习笔记—Site Definition系列--6、基于一个已经部署的List Template来创建List Instance
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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