gFunk
Guest
|
Posted:
Tue Nov 01, 2005 5:51 pm Post subject:
Error: The security validation for this page is invalid. whe |
|
|
I am trying write a WebMethod to copy a file from one document library
to another (regardless if it is in SPS or WSS). I get all the way
through the process up until i try to add the file to the files
collection of the folder of the destination library. Any help would be
greatly appreciated.
*there was quite a bit of trial and error so the code is a bit sloppy.
---- CODE ----
[WebMethod]
public string CopyDocumentFromAreaToSite(string SourceAreaURL, string
SourceDocumentLibrary, string SourceFilePath, string
DestinationSiteURL, string DestinationDocumentLibrary, string
DestinationFilePath)
{
SPWeb sourceWeb = GetWebFromFullURL(SourceAreaURL);
SPFile file = GetFile(sourceWeb, SourceDocumentLibrary,
SourceFilePath);
SPWeb destinationWeb = GetWebFromFullURL(DestinationSiteURL);
SPFolder destinationFolder = GetFolder(destinationWeb,
DestinationDocumentLibrary, DestinationFilePath);
destinationFolder.Files.Add(file.Name, file.OpenBinary(), true);
string ret = file.Name;
return ret;
}
private SPWeb GetWebFromFullURL(string FullURL)
{
SPSite site = new SPSite(FullURL);
SPWeb area;
int areaStartindex = site.Url.Length + 1;
if (FullURL.Length > areaStartindex)
{
string areaName = FullURL.Substring(site.Url.Length + 1);
area= site.AllWebs[areaName];
}
else
{
area = site.OpenWeb();
}
return area;
}
private SPFile GetFile(SPWeb Web, string DocumentLibrary, string
FilePath)
{
SPFolder folder = GetFolder(Web, DocumentLibrary, FilePath);
string fileName = FilePath.Substring(FilePath.LastIndexOf("/") + 1);
return folder.Files[fileName];
}
private SPFolder GetFolder(SPWeb Web, string DocumentLibrary, string
FilePath)
{
SPFolder folder = Web.Folders[DocumentLibrary];
int lastIndex = 0;
int nextIndex= FilePath.IndexOf("/", lastIndex + 1);
while (nextIndex > 0)
{
string folderName = FilePath.Substring(lastIndex + 1, nextIndex -
lastIndex - 1);
folder = folder.SubFolders[folderName];
lastIndex = nextIndex;
nextIndex = FilePath.IndexOf("/", lastIndex + 1);
}
return folder;
} |
|