用于自定义列表的SharePoint备份工具

时间:2015-07-21 07:48:29

标签: powershell sharepoint sharepoint-2013 csom

我有一个包含三个自定义列表的SharePoint 2013文档库。 每天一次,我想将自定义列表备份为excel文档。

SharePoint 2013中是否有内置功能可以配置为定期任务?

或者应该使用PowerShell或CSOM编写脚本或由Windows任务运行的应用程序?

1 个答案:

答案 0 :(得分:0)

你没有任何OOB功能来做这个,我有相同的请求,我写了一个实用程序 - PFB代码 - 这将给你一个o / p在.csv文件

课程计划

{

私有静态DataTable dataTable;

私有静态SPList列表;

static void Main(string [] args)

{

尝试

{

Console.WriteLine("Site Url: "); 

string _siteUrl = Console.ReadLine(); 

if (!string.IsNullOrEmpty(_siteUrl))

{ 

SPSecurity.RunWithElevatedPrivileges(delegate() 

 { 

  using (SPSite site = new SPSite(_siteUrl)) 

   { 

     if (site != null) 

    { 

    SPWeb web = site.RootWeb; 

      if (web != null) 

      { 


       // Export List code segment



        Console.WriteLine("List Name:"); 

        string _listName = Console.ReadLine(); 

         if (!string.IsNullOrEmpty(_listName)) 

         { 

           list = web.Lists[_listName]; 



           if (list != null) 

           { 

             dataTable = new DataTable(); 



             //Adds Columns to SpreadSheet 

              InitializeExcel(list, dataTable); 



              string _schemaXML = list.DefaultView.ViewFields.SchemaXml; 



              if (list.Items != null && list.ItemCount > 0) 

              { 

               foreach (SPListItem _item in list.Items) 

               { 

                 DataRow dr = dataTable.NewRow(); 

                 foreach (DataColumn _column in dataTable.Columns) 

                 { 

if(dataTable.Columns [_column.ColumnName]!= null&& _item [_column.ColumnName]!= null)

                  { 

dr [_column.ColumnName] = _item [_column.ColumnName] .ToString();

                  } 

                 } 

                 dataTable.Rows.Add(dr); 



                } 

               } 



             } 

          } 

System.Web.UI.WebControls.DataGrid grid = new System.Web.UI.WebControls.DataGrid();

      grid.HeaderStyle.Font.Bold = true; 

      grid.DataSource = dataTable; 

      grid.DataBind(); 



      using (StreamWriter streamWriter = new StreamWriter("C:\\" + list.Title + ".xls", false, Encoding.UTF8)) 

      { 

       using (HtmlTextWriter htmlTextWriter = new HtmlTextWriter(streamWriter)) 

       { 

         grid.RenderControl(htmlTextWriter); 

       } 

      } 



         Console.WriteLine("File Created"); 



        #endregion 

       } 

      } 

    } 

    }); 

   } 

  } 

  catch (Exception ex) 

  { 

     Console.WriteLine("Error: " + ex.Message); 

  } 



  Console.ReadLine(); 

} 

//创建excel funution

public static void InitializeExcel(SPList list,DataTable _datatable)

{

if (list != null) 

{ 

 string _schemaXML = list.DefaultView.ViewFields.SchemaXml; 

 if (list.Items != null && list.ItemCount > 0) 

 { 

  foreach (SPListItem _item in list.Items) 

  { 

   foreach (SPField _itemField in _item.Fields) 

   { 

    if (_schemaXML.Contains(_itemField.InternalName)) 

    { 

      if (_item[_itemField.InternalName] != null) 

      { 

       if (!_datatable.Columns.Contains(_itemField.InternalName)) 

       { 

         _datatable.Columns.Add(new DataColumn(_itemField.StaticName, Type.GetType("System.String"))); 

       } 

      } 

     } 

    } 

   } 

  } 

 } 

} 

}