用于删除Sharepoint文档库中超过一天的文件的脚本

时间:2023-01-15 10:32:19

I need a script which can delete all files which are older than a day and this script needs to be invoked every day automatically in the Sharepoint server. How can i do this and any hints as how to write the script?

我需要一个可以删除所有超过一天的文件的脚本,并且需要在Sharepoint服务器中自动调用此脚本。我该怎么做以及如何编写脚本的任何提示?

4 个解决方案

#1


I think an easier way to do it would be to create a site collection policy with an expiration. Set the retention period for one day. You can then attach the disposition workflow to your list which acn be used to clean these files up. You should be able to do all of this without writing any code.

我认为更简单的方法是创建一个过期的网站集策略。将保留期限设置为一天。然后,您可以将处置工作流程附加到列表中,该列表可用于清理这些文件。您应该能够在不编写任何代码的情况下完成所有这些操作。

Here is a link with more information about disposition workflow. http://office.microsoft.com/en-us/sharepointserver/HA101544291033.aspx

这是一个链接,其中包含有关处置工作流的更多信息。 http://office.microsoft.com/en-us/sharepointserver/HA101544291033.aspx

Thanks, Corey

#2


If you really need a script rather than writing code (such as a timer job) then use Powershell to access the SharePoint .NET objects with a scripting engine. Once you've written the script, set up a Windows scheduled task to run it every day.

如果您确实需要脚本而不是编写代码(例如计时器作业),那么使用Powershell通过脚本引擎访问SharePoint .NET对象。编写完脚本后,设置Windows计划任务以每天运行它。

In the script follow Lars' guidance on using one of those two query classes. Then from the query results you can obtain a reference to each SPListItem you'd like to delete. Either use SPListItem.Delete or SPListItem.Recycle to remove the item.

在脚本中遵循Lars关于使用这两个查询类之一的指导。然后,从查询结果中,您可以获取对要删除的每个SPListItem的引用。使用SPListItem.Delete或SPListItem.Recycle删除项目。

Here's an example that uses SPQuery:

这是一个使用SPQuery的示例:

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

$site = new-object Microsoft.SharePoint.SPSite("http://yoursite/")
$web = $site.OpenWeb("Web Title")
$documentLibrary = $web.Lists["Document Library Name"]

$yesterdaysDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([System.DateTime]::UtcNow.AddDays(-1.0))
$query = new-object Microsoft.SharePoint.SPQuery
$query.ViewFields = "<FieldRef Name='Modified' />"
$query.Query = "<Where><Leq><FieldRef Name='Modified' /><Value Type='DateTime' IncludeTimeValue='TRUE'>" + $yesterdaysDate + "</Value></Leq></Where>"
$queryItems = $documentLibrary.GetItems($query)
foreach ($item in $queryItems)
{
    echo $item.Url
    $item.Delete()
}
$web.Dispose()
$site.Dispose()

#3


You could create a custom timer job in SharePoint using .NET and the WSS API to do the job. Use the SPQuery class to query files by date in a document library. Use the SPSiteDataQuery class if you need to query across multiple document libraries.

您可以使用.NET和WSS API在SharePoint中创建自定义计时器作业来完成工作。使用SPQuery类在文档库中按日期查询文件。如果需要跨多个文档库进行查询,请使用SPSiteDataQuery类。

#4


Here is C# code to delete only files on SharePoint site which are older than a day, to run this every day you need to create SharePoint Timer Job and add below code inside execute method of timer job.

以下是C#代码,用于仅删除SharePoint网站上超过一天的文件,每天运行此操作,您需要创建SharePoint计时器作业并在计时器作业的执行方法中添加以下代码。

        SPSite spSite = new SPSite("http://YourSiteUrl");
        SPWeb oWebsite = spSite.OpenWeb();
            SPListCollection collLists = oWebsite.Lists;

            foreach (SPList oList in collLists)
            {
                if (oList.BaseType == SPBaseType.DocumentLibrary)
                {
                    SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oList;

                    if (!oDocumentLibrary.IsCatalog && oList.BaseTemplate != SPListTemplateType.XMLForm)
                    {


                        SPListItemCollection collListItems = oDocumentLibrary.Items;

                        foreach (SPListItem oListItem in collListItems)
                        {
                            if (oListItem.File != null)
                            {
                                if ((DateTime.Now - oListItem.File.TimeCreated).TotalDays > 1)
                                {
                                   oListItem.Delet();

                                }


                            }



                      }
                    }
                }
            }

#1


I think an easier way to do it would be to create a site collection policy with an expiration. Set the retention period for one day. You can then attach the disposition workflow to your list which acn be used to clean these files up. You should be able to do all of this without writing any code.

我认为更简单的方法是创建一个过期的网站集策略。将保留期限设置为一天。然后,您可以将处置工作流程附加到列表中,该列表可用于清理这些文件。您应该能够在不编写任何代码的情况下完成所有这些操作。

Here is a link with more information about disposition workflow. http://office.microsoft.com/en-us/sharepointserver/HA101544291033.aspx

这是一个链接,其中包含有关处置工作流的更多信息。 http://office.microsoft.com/en-us/sharepointserver/HA101544291033.aspx

Thanks, Corey

#2


If you really need a script rather than writing code (such as a timer job) then use Powershell to access the SharePoint .NET objects with a scripting engine. Once you've written the script, set up a Windows scheduled task to run it every day.

如果您确实需要脚本而不是编写代码(例如计时器作业),那么使用Powershell通过脚本引擎访问SharePoint .NET对象。编写完脚本后,设置Windows计划任务以每天运行它。

In the script follow Lars' guidance on using one of those two query classes. Then from the query results you can obtain a reference to each SPListItem you'd like to delete. Either use SPListItem.Delete or SPListItem.Recycle to remove the item.

在脚本中遵循Lars关于使用这两个查询类之一的指导。然后,从查询结果中,您可以获取对要删除的每个SPListItem的引用。使用SPListItem.Delete或SPListItem.Recycle删除项目。

Here's an example that uses SPQuery:

这是一个使用SPQuery的示例:

[System.Reflection.Assembly]::Load("Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c")

$site = new-object Microsoft.SharePoint.SPSite("http://yoursite/")
$web = $site.OpenWeb("Web Title")
$documentLibrary = $web.Lists["Document Library Name"]

$yesterdaysDate = [Microsoft.SharePoint.Utilities.SPUtility]::CreateISO8601DateTimeFromSystemDateTime([System.DateTime]::UtcNow.AddDays(-1.0))
$query = new-object Microsoft.SharePoint.SPQuery
$query.ViewFields = "<FieldRef Name='Modified' />"
$query.Query = "<Where><Leq><FieldRef Name='Modified' /><Value Type='DateTime' IncludeTimeValue='TRUE'>" + $yesterdaysDate + "</Value></Leq></Where>"
$queryItems = $documentLibrary.GetItems($query)
foreach ($item in $queryItems)
{
    echo $item.Url
    $item.Delete()
}
$web.Dispose()
$site.Dispose()

#3


You could create a custom timer job in SharePoint using .NET and the WSS API to do the job. Use the SPQuery class to query files by date in a document library. Use the SPSiteDataQuery class if you need to query across multiple document libraries.

您可以使用.NET和WSS API在SharePoint中创建自定义计时器作业来完成工作。使用SPQuery类在文档库中按日期查询文件。如果需要跨多个文档库进行查询,请使用SPSiteDataQuery类。

#4


Here is C# code to delete only files on SharePoint site which are older than a day, to run this every day you need to create SharePoint Timer Job and add below code inside execute method of timer job.

以下是C#代码,用于仅删除SharePoint网站上超过一天的文件,每天运行此操作,您需要创建SharePoint计时器作业并在计时器作业的执行方法中添加以下代码。

        SPSite spSite = new SPSite("http://YourSiteUrl");
        SPWeb oWebsite = spSite.OpenWeb();
            SPListCollection collLists = oWebsite.Lists;

            foreach (SPList oList in collLists)
            {
                if (oList.BaseType == SPBaseType.DocumentLibrary)
                {
                    SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oList;

                    if (!oDocumentLibrary.IsCatalog && oList.BaseTemplate != SPListTemplateType.XMLForm)
                    {


                        SPListItemCollection collListItems = oDocumentLibrary.Items;

                        foreach (SPListItem oListItem in collListItems)
                        {
                            if (oListItem.File != null)
                            {
                                if ((DateTime.Now - oListItem.File.TimeCreated).TotalDays > 1)
                                {
                                   oListItem.Delet();

                                }


                            }



                      }
                    }
                }
            }