1. 程式人生 > >SharePoint備份文件

SharePoint備份文件

void 直接 [] iar 保存 ppa ext art str

stp文件:SharePoint的.stp文件

在做一個和SharePoint有關的項目時,由於對SharePoint的unfamiliar,所以客戶發了幾個後綴為.stp的文件將我納悶了半天,不知為何物。

按常理,只知道.stp文件是3D 的一個標準交換文件,需要用AutoCAD、PRO/E或SW等三維處理軟件來打開,但看客戶給我的文件大小非常小,應該不可能是3D文件啊。後來Avrin告訴我stp文件是Sharepoint裏的Template file時我才恍然大悟,孤陋寡聞,慚愧啊....

這裏的stp文件果然是SharePoint裏的模板文件,用於將SharePoint裏的List或site等結構保存下來以便移植到別的SharePoint上。實際上這個stp文件是個Cab文件,可以強行更改其後綴為.cab,直接打開或解壓縮後可以看到裏面的文件,其中有個manifest.xml的XML文件。關於這個的具體描述在另外一篇文章《SharePoint Customization: Using WSS List Templates in SPS Areas》裏說得很詳細,可以參考。


一、導出stp文件

1、打開SharePoint裏的網站,在"Site Actions"下選擇"Site Settings",然後選擇Look and Feel欄下的"Save site as template"即可;

2、在SharePoint裏打開一個List,點擊"Settings"下的"List Settings",在“Permissions and Management”下點擊"Save List as template"即可。

二、導入stp文件創建Site及List

打開SharePoint的Home頁,點擊Site Actions —> Site Settings —> Modify All Site Settings,在Galleries下選擇Site templates或List templates進入Site Template Gallery頁或List Template Gallery頁,將相應的Site stp文件或List stp文件上傳到Gallery。然後在新建網站或新建List時就可以選擇上傳的木板進行創建。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.IO;

using Microsoft.SharePoint;
using CodeArt.SharePoint;
using CodeArt.SharePoint.CamlQuery;

namespace BackFile
{
    class Program
    {
        static void Main(string[] args)
        {

            string SPSiteUrl = ConfigurationManager.AppSettings["SPSiteUrl"];
            string SPWebUrl = ConfigurationManager.AppSettings["SPWebUrl"];
            string SPDocumentLibraryName = ConfigurationManager.AppSettings["SPDocumentLibrary"];
            string BackupPath = ConfigurationManager.AppSettings["BackupPath"];

            SPSite site = null;
            SPWeb rootWeb = null;

            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (site = new SPSite(SPSiteUrl))
                {
                    using (rootWeb = site.OpenWeb(SPWebUrl))
                    {

                    }
                }
            });

            SPDocumentLibrary doclib = (SPDocumentLibrary)rootWeb.Lists[SPDocumentLibraryName];
            MakeFile(doclib.RootFolder, BackupPath);
            Console.ReadLine();

        }

        public static void MakeFile(SPFolder folder, string BackupPath)
        {
            Console.WriteLine("Current Folder:" + folder.Url);

            string folderPath = BackupPath + "\\" + folder.Url;
            if (!System.IO.Directory.Exists(folderPath))
            {
                Console.WriteLine("Create Folder:" + folder.Url);
                Directory.CreateDirectory(folderPath);
            }

            foreach (SPFile file in folder.Files)
            {
                Console.WriteLine("Create File:" + folderPath + "\\" + file.Name);
                byte[] bs = file.OpenBinary();
                File.Create(folderPath + "\\" + file.Name);
            }

            foreach (SPFolder subfolder in folder.SubFolders)
            {
                MakeFile(subfolder, BackupPath);
            }

        }
    }
}

  技術分享

SharePoint備份文件