Use SQL table in Aspose.Word for Sharepoint

Hi,


I have a SharePoint site, and my final goal is to take a SQL database, create word documents from templates (with SQL content), and put them in my SharePoint Library.

I’ve downloaded Aspose.Word.SharePoint, but I fail to see a way to connect my database fields to my Word template.

I’ve then tried one of the demo for Aspose.Word.NET, and with some C# and a XML (hardcoded) document, I’ve been able to create a Word exactly like I want.

The documentation of Aspose.Word for SharePoint make mention of SqlDatSource, but no direct exemple are shown.


My questions are :
  • How can I take the content of my SQL database and put it in my MS Word template?
  • Do I have to use Aspose.Word.SharePoint?
  • Do I have to create a XML from my SQL query and feed it every time to some C# code that would run in some SharePoint workflow?
  • Is there some well explained exemples of that?


Thank you for your time.


Hi Simon,


Thanks for your inquiry and sorry for the delayed response. We’re working over your query and will get back to you as soon as possible.

Best Regards,

Hi Simon,

Thank you for your interest in our products. Please find the answers below.

How can I take the content of my SQL database and put it in my MS Word template?

The most suitable way is via MailMerge feature. You can find an example of Aspose.Words code using an SQL query to fill an MS Word template here:
https://docs.aspose.com/words/net/types-of-mail-merge-operations/

Do I have to use Aspose.Word.SharePoint?

The short answer is “No”.

You can use Aspose.Words or Aspose.Words for SharePoint. They use the same templates. A template is an MS Word file where placeholders to be filled with data are designated with MAILMERGE fields.

Aspose.Words is a class library. You need some code to retrieve your data from a database and put into a DataTable, DataSet or other supported object. Then you call Document.MailMergeExecute() to fill the template.

Aspose.Words for SharePoint is much like a wrapper around Aspose.Words. It integrates into SharePoint and provides user interface and workflow activities for file conversion and making reports. To make a report, you describe you data source and query in an XML file. You link this file with a mail merge template and put both files in a SharePoint document library. Then you can fill the report via user interface or workflow. You can also build the report programmatically via Aspose.Words for SharePoint API.

So I think that in case you are going to have your own code to retrieve the data (or for some other purpose) you should better use Aspose.Words. It’s much more flexible.

If all you need to code is a database connection, query to retrieve your data and probably some parameters, you can try Aspose.Words for SharePoint.

Do I have to create a XML from my SQL query and feed it every time to some C# code that would run in some SharePoint workflow?

The idea behind putting query into an XML file was to avoid writing code. I’m attaching an example of a test report template and definition. You can use Aspose.Words for SharePoint for making reports from such files without writing your own code.

So if you are going to have your own code to query anyway, it’s simpler to use Aspose.Words directly.

Is there some well explained examples of that?

I’ve given a link to Aspose.Words mail merge code example above.

Please check the attached files for an example of a simple Aspose.Words for SharePoint report. If you try to use them, note that the link between .doc template and report description should match the actual file location. Please see how to update the link in this topic:
https://docs.aspose.com/words/sharepoint/linking-report-template-with-report-definition/
if needed.

Hope this helps you. Feel free to ask further questions, I will be glad to help you.

Thanks && Best Regards,

Hi Dmitry Matveenko,

Thank you for your reply. However, at the time of my post, I had already did everything you mentionned in your answer.

In the meantime, I have resolved my issue by setting a connection to my database on MS SQL servers 2008, with the help of this page :

Beginners guide to accessing SQL Server through C#

Now, I’ve been able to make queries on my tables, and show them with the reader. I put the result in a string, wich I put in my mergefields in my template, using a demo from your site (doc.mailmerge.execute(string)).

I then get my .docx, but is there a built-in way to put it in SharePoint, maybe from your method “Make a report via Aspose.Words” ?

If not, do you happen to know a tutorial on How to automatically put documents in a SharePoint Document Library (maybe from a workflow)?

Thank you for your time!

Simon P.

Hi Simon,

It’s great to hear that you solved you problem.

To put a file into a SharePoint document library, you don’t need Aspose.Words for SharePoint. Microsoft provides an API for that in Microsoft.SharePoint namespace.

You can use SPFolder.Files.Add() to add a file. An example here: https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-server/ms412207(v=office.15) might be helpful. Be sure to observe the note on IDisposable interface to avoid a leak.

Hope this helps.

Thanks && Best Regards,