[sharepoint]如何在sps2003下載文件庫檔案並壓縮

[sharepoint]如何在sps2003下載文件庫檔案並壓縮

開發環境

Sharepoint 2003(Framework1.1)

作業

Win2k server x64

使用工具

vs2003

環境跟工具有點舊,怕忘了怎麼做記錄一下步驟:

1. 加入參考sharepoint.dll,檔案位置在

C:\Program Files (x86)\Common Files\Microsoft Shared\Web Server Extensions\60\ISAPI

如果作業系統為32位元路徑為

C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\ISAPI

操作界面如下

clip_image001

clip_image002

2. 建立類別

clip_image003

clip_image005

3. 參照sps2003 sdk撰寫程式碼


using System.Text;
using System.Collections;
using Microsoft.SharePoint;
using System.Configuration;
using System.IO;
using System.Diagnostics;

namespace SPSDocLibrary
{
	/// <summary>
	/// 管理sps的文件庫
	/// </summary>
	public class DocLibraryCollect
	{
		private string[] arealists;//區域列表
		private string[] listlists;//清單列表
		private SPSite site = new SPSite("http://127.0.0.1");// 網址
		private string areaName;
		private string listName;
		private string siteurl;//網頁網址
		private string folder;//存放檔案的根目錄		
		private string zipfilename;//壓縮檔名
		public DocLibraryCollect()
		{
			areaName = ConfigurationSettings.AppSettings["AreaName"];
			listName = ConfigurationSettings.AppSettings["ListName"];
			siteurl = ConfigurationSettings.AppSettings["DefaultSite"];
			folder = ConfigurationSettings.AppSettings["DefaultFolder"];
			zipfilename = ConfigurationSettings.AppSettings["CompressFileName"];
            arealists = areaName.Split(',');
            listlists = listName.Split(',');
			Console.Out.WriteLine("區域:" + this.areaName);
			Console.Out.WriteLine("清單:" + this.listName);
		}
		public void StartProcess()
		{							
			foreach(string selectedsite in arealists)
			{
				SPWeb tempsite = site.AllWebs[selectedsite];  
				string replaceword = string.Empty;
				//將網站名稱格式由cxx\網站取代成 網站
				if(selectedsite.IndexOf("/")!=-1)
				{
					replaceword = "/"+ selectedsite.Substring(0,selectedsite.IndexOf("/"));
				}
				SPListCollection lists = tempsite.Lists;
				foreach (SPList list in lists)
				{

					if (list.BaseType == SPBaseType.DocumentLibrary)
					{
						//bool titleexist = false;
						SPDocumentLibrary docLibrary = (SPDocumentLibrary)list;						
						foreach(string selectedlist in listlists)
						{							
							if (list.Title ==selectedlist)
							{
														
								string listpath = list.DefaultView.Url.Split('/')[0];
								//選取所有文件
								if (!docLibrary.IsCatalog && list.BaseTemplate != SPListTemplateType.XMLForm)
								{
									
									SPListItemCollection docLibItems = docLibrary.Items;									
									foreach (SPListItem docLibItem in docLibItems)
									{
										string downloadurl = siteurl + docLibItem.File.ServerRelativeUrl;
										string savingpath = (replaceword == string.Empty)? 
											folder+ docLibItem.File.ServerRelativeUrl.Replace(listpath,list.Title).Replace(tempsite.Name,tempsite.Title).Replace("/","\\\\"):
											folder+ docLibItem.File.ServerRelativeUrl.Replace(replaceword,string.Empty).Replace(listpath,list.Title).Replace(tempsite.Name,tempsite.Title).Replace("/","\\\\");
										string dicpath = savingpath.Replace(docLibItem.File.Name,string.Empty);
										DirectoryInfo di = new DirectoryInfo(dicpath);
										if(di.Exists == false)
										{
											Directory.CreateDirectory(di.FullName);
										}										
										DownloadUpLoadUtility.GetBytesByUrl(downloadurl , savingpath );
									}
								}
							}
						}
						//當網址為C18/trust時,要取得文件庫所有資料
						if(selectedsite == "C18/trust")
						{
							string listpath = list.DefaultView.Url.Split('/')[0];
							if (!docLibrary.IsCatalog && list.BaseTemplate != SPListTemplateType.XMLForm)
							{
									
								SPListItemCollection docLibItems = docLibrary.Items;									
								foreach (SPListItem docLibItem in docLibItems)
								{
									string downloadurl = siteurl + docLibItem.File.ServerRelativeUrl;
									string savingpath = (replaceword == string.Empty)? 
										folder+ docLibItem.File.ServerRelativeUrl.Replace(listpath,list.Title).Replace(tempsite.Name,tempsite.Title).Replace("/","\\\\"):
										folder+ docLibItem.File.ServerRelativeUrl.Replace(replaceword,string.Empty).Replace(listpath,list.Title).Replace(tempsite.Name,tempsite.Title).Replace("/","\\\\");
									string dicpath = savingpath.Replace(docLibItem.File.Name,string.Empty);
									DirectoryInfo di = new DirectoryInfo(dicpath);
									if(di.Exists == false)
									{
										Directory.CreateDirectory(di.FullName);
									}									
									DownloadUpLoadUtility.GetBytesByUrl(downloadurl , savingpath );
								}
							}							
						}
					}
				}				
			}						
			Process.Start("BackUpWeb.Bat");
		}
		
	}
}

下載功能


using System.Net;

namespace SPSDocLibrary
{
	/// <summary>
	/// 
	/// </summary>
	public class DownloadUpLoadUtility
	{
		public DownloadUpLoadUtility()
		{
			// 
			// TODO: 在此加入建構函式的程式碼
			//
		}
		public static void GetBytesByUrl(string url, string path)
		{
			try
			{
				WebClient client = new WebClient();
				NetworkCredential credentials = new NetworkCredential("account", "password","domain");
				client.Credentials = credentials;
				client.Headers.Add("Accept-Language: en-us,en;q=0.5");
				client.Headers.Add("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
				// client.Headers.Add("Accept-Encoding: gzip,deflate");
				client.Headers.Add("Keep-Alive: 300");
				client.Headers.Add("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
				client.DownloadFile(url, path);                    
			}
			catch (WebException ex)
			{
				Console.WriteLine(ex.ToString());
				Console.Read();
			}
		}		
	}
}

App.Config內容


<configuration>
   <appSettings>
	<add key="AreaName" value="0051,C3/0551,C7/0251,C5/0301,C15/1230,C10/0451,C11/0501,C14/1094,C12/0277,C9/0601,C18/trust"/>
	<add key="ListName" value="測試文件庫檔案"/>		
	<add key="DefaultSite" value="http://localhost"/>
	<add key="DefaultFolder" value="C:\\test\\"/>
	<add key="WebFolder" value="C:\\Program Files (x86)\\Common Files\\Microsoft Shared\\Web Server Extensions\\60\\TEMPLATE\\LAYOUTS\\sps\\"/>
	<add key="CompressFileName" value="測試文件庫檔案.rar"/>
   </appSettings>  
</configuration>

4. 因為1.1framework沒有壓縮的類別所以用7-ZIP來作

產生一個批次檔將所有檔案壓縮


@ECHO off
ECHO.
ECHO.
ECHO          ===== 正在備份資料 =====


del C:\test\測試文件庫檔案.rar
7z a C:\test\測試文件庫檔案.rar C:\test\*


ECHO.
ECHO.          ===== 備份完畢 =====
ECHO.
ECHO.

這樣就ok了

記得程式要部署在sharepoint主機上

參考資料

SharePoint Products and Technologies 2003 Software Development Kit (SDK)

[MOSS 開發]

如何使用7-zip 指令加壓及排程備份檔案| 囧部落

C# 用 WebClient 模擬 IE 6.0 連線