获取站点下所有的文档
private static List<SPListItem> GetSiteDocItems(SPSite site)
{
List<SPListItem> docItemList = new List<SPListItem>();
SPWebCollection webcollection = GetWebSiteCollection(site);
foreach (SPWeb web in webcollection)
{
SPListCollection listcollection = web.Lists;
foreach (SPList list in listcollection)
{
if (list.BaseType == SPBaseType.DocumentLibrary && list.BaseTemplate != SPListTemplateType.ListTemplateCatalog
&& list.BaseTemplate == SPListTemplateType.DocumentLibrary)
{
SPListItemCollection itemcollection = GetSiteListItemsCollection(list);
foreach (SPListItem item in itemcollection)
{
//if (item.ParentList.Title == "文档")
if (item.ParentList.Title == "DMSShareLib")
{
docItemList.Add(item);
}
}
}
}
}
return docItemList;
}
站点下所有文档的全文搜索
#region SearchFullText
public DataTable SearchDocuments(string strKey, string fieldName, string scopeFolderUrl)
{
string strSiteName = @"/sites/" + SPSHelper.GetSiteName;
SPQuery query = new SPQuery();
try
{
SPWeb web = new SPSite(strServerURL).AllWebs[strSiteName];
SPList tasks = web.Lists[GetTempDocLibName];
SPFolder folder = web.Folders[scopeFolderUrl];
query.Folder = folder;
query.Query = "<Where><Eq><FieldRef Name='" + fieldName + "'/><Value Type='Text'>" + strKey + "</Value></Eq></Where>";
SPListItemCollection items = tasks.GetItems(query);
return items.GetDataTable();
}
catch (Exception ex)
{
throw ex;
}
finally
{
}
}
/// <summary>
///
/// </summary>
/// <param name="strKey"></param>
/// <param name="AttribScope"></param>
/// <returns></returns>
public SPSearchResultCollection SearchDocuments(string strKey, string AttribScope)
{
SPWeb site = null;
SPGlobalAdmin globAdmin = null;
System.Uri uri = null;
SPVirtualServer virtualServer = null;
SPSiteCollection siteCollections = null;
string SiteName = SPSHelper.GetSiteName;
string Site = @"/sites/" + SPSHelper.GetSiteName;
try
{
globAdmin = new SPGlobalAdmin();
uri = new System.Uri(strServerURL);
virtualServer = globAdmin.OpenVirtualServer(uri);
siteCollections = virtualServer.Sites;
SPWeb web = new SPSite(strServerURL).OpenWeb(Site);
SPSearchResultCollection oRootDocResults = web.SearchListItems(strKey);
return oRootDocResults;
}
finally
{
if (site != null)
site.Close();
}
}
#endregion SearchFullText
将文件从一个站点copy到另一个站点,可以用在内网发布到外网
private bool CopyFileCrossSite(string strSourceFileUrl, string strDescSiteUrl)
{
string delimStr = "/";
char[] delimiter = delimStr.ToCharArray();
string[] split = null;
string strNewUrl1 = strServerURL + strDescSiteUrl;
strSourceFileUrl = strServerURL + strSourceFileUrl;
split = strDescSiteUrl.Split(delimiter);
string strSite = "/" + split[1] + "/" + split[2];
string strFileName = split[4];
System.Uri myURi = null;
WebRequest req = null;
WebResponse res = null;
Stream inStream = null;
SPSite portal = null;
SPWeb site = null;
try
{
myURi = new System.Uri(strSourceFileUrl);
req = WebRequest.Create(myURi);
req.Method = "GET";
req.Timeout = System.Threading.Timeout.Infinite;
CredentialCache myCache = new CredentialCache();
myCache.Add(myURi, "NTLM", new NetworkCredential(SPSHelper.GetSpsUid, SPSHelper.GetSpsPwd, SPSHelper.GetDomainName));
req.Credentials = myCache;
res = req.GetResponse();
inStream = res.GetResponseStream();
long length = res.ContentLength;
portal = new SPSite(strServerURL);
site = portal.AllWebs[strSite];
site.AllowUnsafeUpdates = true;
byte[] contents = new byte[length];
int readnum;
int num;
readnum = 0;
while (readnum < length)
{
num = inStream.Read(contents, readnum, (int)(length - readnum));
readnum += num;
}
if (site.GetFile(strNewUrl1).Exists)
{
//TODO删除文件
}
site.Files.Add(strNewUrl1, contents);
}
catch (Microsoft.SharePoint.SPException msg2)
{
errStr = msg2.Message.ToString();
return false;
}
catch (System.UriFormatException)
{
errStr = "88";
return false;
}
catch (Exception msg1)
{
errStr = msg1.Message.ToString();
return false;
}
finally
{
myURi = null;
req = null;
res = null;
if (inStream != null)
inStream.Close();
if (portal != null)
portal.Close();
if (site != null)
site.Close();
}
return true;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器