Unable to use Aspose.Cells API in SP2010 EventReceiver project

Hi. I am trying to link to Aspose license key file in my Visual Studio 2010 C# code (inside Sharepoint 2010 event receiver event), but the following line in red causes problems:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using System.IO;
using Microsoft.SharePoint.Client;
using Aspose.Cells;
public class EventReceiver1 : SPItemEventReceiver
{
public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
Aspose.Cells.License license = new Aspose.Cells.License();
// license.SetLicense("\\servername\inetpub\asposelic\Aspose.Cells.lic"); // line commented out
// more Aspose.Cells code to be added later…
}
}

I have the Aspose.Cells reference in my Visual Studio 2010 project and it looks fine. Am I missing something here? Do I need to follow any additional steps in order to be able to use the Aspose.Cells API? Please provide me some clear step-by-step instructions on how to resolve this issue. I am using Visual Studio 2010 and SharePoint 2010 on Windows 7 64-bit OS.


Thanks!

Hi,

Thanks for your posting and using Aspose.Cells.

What error are you getting? Did you add a reference to Aspose.Cells assembly in your project?

Please also see Aspose.Cells for SharePoint documentation.

Documentation - Aspose.Cells for SharePoint

I am not getting any error messages. My code simply doesn’t execute. Yes, I have a reference to Aspose.Cells.dll in my project.

Is there anything else that is required to be able to use Aspose.Cells API with SharePoint 2010??



Hi,

Can you please confirm to me if you can use any other .NET framework assembly in your SharePoint Project or SharePoint Library Events.

Can you execute such a code, this code is not using any Aspose.Cells code but it uses System.IO library.

If you can successfully execute this code, then you can also use Aspose.Cells assembly code.

Please Note:
Aspose.Cells
is a .NET assembly if you can use other .NET framework assemblies fine then you can also use this one.

C#


string filePath = @“c:\source.xlsx”;


byte[] bytes = File.ReadAllBytes(filePath);


File.WriteAllBytes(“c:\output.xlsx”, bytes);


I tried this code:

string filePath = @“c:\temp\source.xlsx”;
byte[] bytes = File.ReadAllBytes(filePath);
File.WriteAllBytes(“c:\temp\output.xlsx”, bytes);

But I am getting the following error:

Request for the permission of type ‘System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

However, the following code works fine until I include the line in red:

public override void ItemUpdated(SPItemEventProperties properties)
{
base.ItemUpdated(properties);
try
{
EventFiringEnabled = false;
SPList list = properties.List;
SPListItem item = properties.ListItem;
SPFile file = properties.ListItem.File;
string target = “”;
SPFile newPdfFile;

// string filePath = @“c:\temp\source.xlsx”;
// byte[] bytes = File.ReadAllBytes(filePath);
// File.WriteAllBytes(“c:\temp\output.xlsx”, bytes);

if ((item.File.Name.EndsWith(".xls") || item.File.Name.EndsWith(".xlsx")) && item.File.Level == SPFileLevel.Published && item.ModerationInformation.Status == SPModerationStatusType.Approved)
{
target = item.File.Url.Replace(".xlsx", “.pdf”).Replace(".xls", “.pdf”); // for now just create a copy with pdf extension

Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense("\\arlwebstaging01\inetpub\asposelic\Aspose.Cells.lic");
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(file.Name);
wb.Save(file.Name.Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);

item.File.CopyTo(target, true);

newPdfFile = properties.Web.GetFile(target);

if (newPdfFile.RequiresCheckout) newPdfFile.CheckOut();
newPdfFile.Item[“PDFOnly”] = false;
newPdfFile.Item.Update();
if (newPdfFile.RequiresCheckout) newPdfFile.CheckIn(“Automated Check-In”);
newPdfFile.Publish(“Automated Publishing”);
newPdfFile.Approve(“Automated Approval”);
}
catch (Exception e)
{
throw (e);
}
finally
{
EventFiringEnabled = true;
}
}


Any suggestions?






Hi,

We could not directly access the file for the issue of FileIOPermission.

And please set CellsHelper.FontDir property, otherwise we could not get the font settings.

Please try the following code:

private void ConvertFile(SPFile sourceFile, SPFolder destFolder, string destFileName, SaveFormat destFileFormat, bool overwrite)
{

try
{

Stream inputStream = sourceFile.OpenBinaryStream();

Stream outputStream = new MemoryStream();

string sourceExt = Path.GetExtension(sourceFile.Name).ToLower();


try
{

     Workbook book = <span class="kwrd">new</span> Workbook(inputStream);<br>
     book.Save(outputStream, SaveFormat.Pdf);<br>
  }<br>      <span class="kwrd">catch</span> (Exception e)<br>      {<br>
     <span class="kwrd">throw</span> <span class="kwrd">new</span> ApplicationException("Convertion Failed. " + e.Message, e);<br>

  }<br>
  outputStream.Seek(0, SeekOrigin.Begin);<br>
  destFolder.Files.Add(destFolder.Url + "/" + destFileName, outputStream, overwrite);<br>

}
catch (ArgumentException)
{
throw;

}
catch
{
throw;
}


}


BTW, you can try Aspose.Cells for SharePoint :
( http://www.aspose.com/community/files/73/sharepoint-components/aspose.cells-for-sharepoint/entry349440.aspx )



I tried your code but I am getting the following error:

[System.IO.FileLoadException] = {“Could not load file or assembly ‘Aspose.Cells, Version=7.1.2.7, Culture=neutral, PublicKeyToken=716fcc553a201e56’ or one of its dependencies. An operation is not legal in the current state. (Exception from HRESULT: 0x80131509)”:"Aspose.Cells, Version=7.1…

See image attached to this post.

Do you have any suggestions on why this is not working? Could you provide clear instructions on how to use Aspose.Cells dll with SharePoint 2010? Is adding a project reference enough to be able to use the API or are there any additional steps required??

Thanks!


Hi,

Just adding a reference should be ok, there is no dependency of Aspose.Cells for .NET.

I have already logged your issue in our database and forwarded your issue to development team.

Now, I have added your comment/feedback against the issue id: SHAREPOINT-40008

We will get back to you asap.

Hi,

Please register the DLL in GAC.

I registered Aspose DLL in GAC and the line in blue does not generate errors anymore.

However, I am getting ‘Null reference’ error on the line in red below:

Aspose.Cells.License license = new Aspose.Cells.License();
string strLicenseFile = @"\servername01\inetpub\asposelic\Aspose.Cells.lic";
//string strLicenseFile = “C:\Projects\EventReceiverSolution\Aspose\Aspose.Cells.lic”; // same error
license.SetLicense(strLicenseFile);

Any suggestions on how to fix this? The lic file is located on the web server and on C: and the paths should work…

If there is another way to reference the license file, please provide me with some clear instructions.


Thanks!


Hi,

Please try providing a physical path not a mapped or virtual path.

So, if your license is in C:\myFolder and license file name is Aspose.Cells.lic, then your code will look like this.

Make sure, you have a read access to C:\myFolder\Aspose.Cells.lic file.

C#


license.SetLicense(@“C:\myFolder\Aspose.Cells.lic”);

Hi,

a) Please use the latest fix: Aspose.Cells
for .NET v7.2.0.4



b) Please use License.SetLicense(stream ) method.

I am able to open the license file now, but I am not able to open my Excel workbook using Aspose.Cells.

I am trying to convert XLS to PDF in SharePoint 2010. Here is my code (line in red causes error):

fileAsposeLicense = properties.Web.GetFile(“CM/Aspose.Cells.lic”);
if (fileAsposeLicense.Exists)
{
byte[] data = fileAsposeLicense.OpenBinary();
MemoryStream licenseStream = new MemoryStream(data, 0, data.Length);
licenseStream.Seek(0, SeekOrigin.Begin);
Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense(licenseStream);
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(file.Name);
wb.Save(file.Name.Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);
licenseStream.Close();
}

Error: "Request for the permission of type 'System.Security.Permissions.FileIOPermission failed"

I changed the trust level to ‘Full’ in my config.web but it did not fix the issue.

Please let me know how this IO error can be resolved. Thanks!


Hi,


I think you may try to load the Excel file in the same way (using the similar code/apis) as you have done with license file which works fine, it should also work fine on your SP2010 environment.
e.g

fileExcel = properties.Web.GetFile(“CM/MyFile.xls”);
byte[] filedata = fileExcel.OpenBinary();
MemoryStream fileStream = new MemoryStream(filedata, 0, filedata.Length);
fileStream.Seek(0, SeekOrigin.Begin);
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(fileStream);

Thanks. That worked. However, I still get the same IO error when trying to convert XLS to PDF:

Request for the permission of type ‘System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089’ failed.

Here is the line that produces the error:

wb.Save(file.Name.Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);

Do you have any suggestions on how to resolve this problem? Thanks!






Hi,

Replace this line with the following code and let me know if it works fine now.

wb.Save(file.Name.Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);

C#


Stream outputStream = new MemoryStream();

wb.Save(ms, SaveFormat.Pdf);


outputStream.Seek(0, SeekOrigin.Begin);

destFolder.Files.Add(destFolder.Url + “/” + file.Name + “.out.pdf”, outputStream, overwrite);



What is ‘ms’ in the following line: wb.Save(ms, SaveFormat.Pdf);

Should it be replaced with outputStream ??


Thanks!


Can this ‘save’ code be used in a sandboxed solution?

developer2010:
What is 'ms' in the following line: wb.Save(ms, SaveFormat.Pdf);

Should it be replaced with outputStream ??


Thanks!

Hi,

Yes, it is typo, you should use outputstream actually.

The SAVE method in your code throws a ‘null reference’ error. Please take a look at the attached image and let me know how to resolve this problem. Thanks!

FYI: I have a sandboxed solution. Here is the code:

SPList list = properties.List;
SPListItem item = properties.ListItem;
SPFile file = properties.ListItem.File;
SPFile fileAsposeLicense;
SPFolder libraryFolder = item.Folder;

fileAsposeLicense = properties.Web.GetFile(“Aspose.Cells.lic”);

if (fileAsposeLicense.Exists)
{
byte[] data = fileAsposeLicense.OpenBinary();
MemoryStream licenseStream = new MemoryStream(data, 0, data.Length);
licenseStream.Seek(0, SeekOrigin.Begin);

Aspose.Cells.License license = new Aspose.Cells.License();
license.SetLicense(licenseStream);

byte[] filedata = file.OpenBinary();
MemoryStream fileStream = new MemoryStream(filedata, 0, filedata.Length);
fileStream.Seek(0, SeekOrigin.Begin);
Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(fileStream);

Stream outputStream = new MemoryStream();
wb.Save(outputStream, SaveFormat.Pdf); // this line fails

///outputStream.Seek(0, SeekOrigin.Begin);
///libraryFolder.Files.Add(libraryFolder.Url + “/” + file.Name + “.out.pdf”, outputStream, true);

///Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook(file.Name);
///wb.Save((file.Name.Replace(".xlsx", “.pdf”)).Replace(".xls", “.pdf”), Aspose.Cells.SaveFormat.Pdf);

licenseStream.Close();
}

return;