获取站点下所有的文档
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;
}