Creating unified file programatically

Vote:
 

Hi all,

I'm working on a project and hope someone will be able to helpme with it.

I'm migrating a website from a custom CMS into Episerver, conmplete with lots of files. My problem is how to save the files as unified files during the import process.

First of all, with this piece of code:

PageData page = DataFactory.Instance.GetDefaultPageData(PageReference.StartPage, "Publication");

int folderId = (int)page.Property["PageFolderID"].Value;

string pageDirectoryRootVirtualPath = VirtualPathHandler.PageDirectoryRootVirtualPath;
IPageDirectory pageDirectory = HostingEnvironment.VirtualPathProvider.GetDirectory(pageDirectoryRootVirtualPath) as IPageDirectory;
string virtualPathFromFolderId = VirtualPathUtilityEx.Combine(pageDirectoryRootVirtualPath, VirtualPathUtility.AppendTrailingSlash(folderId.ToString()));

UnifiedDirectory directory = HostingEnvironment.VirtualPathProvider.GetDirectory(virtualPathFromFolderId) as UnifiedDirectory;
if (directory == null)
{
directory = pageDirectory.CreateSubdirectory(folderId.ToString(), page);
}

giving me an error saying I don't have permission tocreate subdirectory

Secondly, this bit of code

WebClient client = new WebClient();
byte[] response = client.DownloadData(filePath);

 gives me byte array of the file, but how do I turn this into a unified file and save it into the directory?

Any help/code sample will be greatly appreciated.

 

Thanks!

 

#31660
Aug 05, 2009 17:25
Vote:
 

For the permission error I don't know. Could it be that you have to save the page first, before trying to create the page's folder? I don't remember... Edit: Use the GetPageDirectory method of your PageData object.

To write a new file, call the CreateFile of your UnifiedDirectory. Then call Open on the returned UnifiedFile to get a Stream which you can write your bytes to.

#31668
Edited, Aug 06, 2009 8:44
Vote:
 

Fantastic, thansk a lot. For anyone intersted here is the final code I used:

 

UnifiedDirectory directory = page.GetPageDirectory(true);
UnifiedFile _file = directory.CreateFile(Path.GetFileName(filePath));
Stream s = _file.Open(FileMode.Create, FileAccess.Write);
s.Write(file, 0, file.Length);
s.Close(); 

#31676
Aug 06, 2009 15:33
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.