Use query string parameters to create a document with metadata

A few weeks ago a customer asked me if there was a solution to create a document in SharePoint by using the url. He wanted to create a contract for each of his customers by using the url and a few query string parameters (to add metadata to the document).

I did not found a way to do this out-of-the-box so I suggested to create a sandboxed solutions that will handle the request.

What do you need to start?

  • a SharePoint site
  • a document library with
    • a title column (that’s in there by default)
    • a customer column (text – single line), we will add the name of the customer here
    • a modified template document (format it like you wish, I added a title and the customer fields), note: make sure you save the template document as a .docx file, not as a .dotx file.
  • Visual Studio 2010

What will we do?

We will create a web part that we will deploy as a sandboxed solution. The web part will accept the query string parameters and create a document in the library.

To begin, create a new empty SharePoint project and choose to deploy it as a sandboxed solution.

image

image

In your project add a new Web Part. Call it: “ParameterPortalWebPart”

image

Now we have to add some code:

Protected Overrides Sub CreateChildControls()

    Dim label As New Label

    label.Text = "<h1>Parameter portal WebPart</h1>"

    Dim textboxOutput As New TextBox
    textboxOutput.TextMode = TextBoxMode.MultiLine
    textboxOutput.Rows = 10
    textboxOutput.Columns = 20

    Dim output As New Text.StringBuilder()

    output.Append("Customer: ").Append(Page.Request.QueryString("customer"))
    output.Append(Environment.NewLine)
    output.Append("Title: ").Append(Page.Request.QueryString("title"))
    output.Append(Environment.NewLine)
    output.Append(Environment.NewLine)

    ‘create file
    Dim filename As String
    Dim fileurl As String

    If Page.Request.QueryString("customer") <> String.Empty And Page.Request.QueryString("title") <> String.Empty Then
        filename = String.Format("{0}.{1}.{2}", Page.Request.QueryString("customer"), Page.Request.QueryString("title"), Now.Ticks)

        fileurl = Me.CreateDocument(filename, "Document", "Contracts", _
                                                  Page.Request.QueryString("customer"), _
                                                  Page.Request.QueryString("title"))

    End If
 

    Dim hyperlinkFileUrl As New HyperLink
    hyperlinkFileUrl.Text = filename
    hyperlinkFileUrl.NavigateUrl = fileUrl

    textboxOutput.Text = output.ToString

    Me.Controls.Add(label)
    Me.Controls.Add(textboxOutput)
    Me.Controls.Add(hyperlinkFileUrl)

End Sub

 

What do we do here? We add a few controls to the web part (label, textbox) to provide some feedback. Then we make sure the query string parameters are not empty. Next, we generate a unique filename (by using the Now.Ticks property). You could use another method to do this but this is out of scope here.

Then we call the CreateDocument method that returns the url of the newly created document. This function contains the actual code to generate a document:

Private Function CreateDocument(ByVal filename As String, ByVal contenttypename As String, ByVal listname As String, _
                            ByVal customerName As String, ByVal title As String) As String

    Try
        Dim site As SPSite = SPContext.Current.Site
        Using web As SPWeb = site.OpenWeb()
            web.AllowUnsafeUpdates = True

            Dim list As SPList = web.Lists(listname)
            Dim folder As SPFolder = web.Folders(listname) ‘root folder
            Dim files As SPFileCollection = folder.Files

         
            Dim template As String = list.ContentTypes(contenttypename).DocumentTemplateUrl

            Dim destinationFile As String = files.Folder.Url & "/" & filename & ".docx"

            Dim nieuwDocument As SPFile = files.Add(destinationFile, web.GetFile(template).OpenBinary(), True)
            Dim newItem As SPItem = nieuwDocument.Item
            newItem("ContentType") = contenttypename
            newItem("Customer") = customerName
            newItem("Title") = title
            newItem.Update()

            Return nieuwDocument.Url

        End Using
    Catch ex As Exception
        Throw ex
    End Try
End Function

 

In this chunk of code we get the document library (root folder) and get the url of the template for a certain content type in that library. We will add a new document based on that template document. At the end we populate the custom fields with the parameters that the url query string provided us.

You can use Visual Studio the deploy the solution to you development environment and start play with it. Package the project and upload the .wsp file to your online environment if you want to test it there.

Have fun with it.

* this code was part of a SharePoint development training. You want this training in your company? Contact us!