web services code examples web services code examples in the following chapters some code examples...

8
Web Services Code Examples

Upload: vuongliem

Post on 23-May-2018

246 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Web Services Code Examples Web Services Code Examples In the following chapters some code examples explain how to use the SAPERION web service by using the IDE Visual Studio 2008 and

Web Services Code Examples

Page 2: Web Services Code Examples Web Services Code Examples In the following chapters some code examples explain how to use the SAPERION web service by using the IDE Visual Studio 2008 and

Copyright © 2016 Lexmark. All rights reserved.

Lexmark is a trademark of Lexmark International, Inc., registered in the U.S. and/or other countries. All other trademarksare the property of their respective owners. No part of this publication may be reproduced, stored, or transmitted in anyform without the prior written permission of Lexmark.

Page 3: Web Services Code Examples Web Services Code Examples In the following chapters some code examples explain how to use the SAPERION web service by using the IDE Visual Studio 2008 and

Table of Contents

1 Login Example ..................................................................................................... 2

1.1 Creating AuthenticationService Stubs ............................................................ 2

1.2 Logging in a User ........................................................................................... 2

1.3 Archive a Document ....................................................................................... 3

1.4 Searching documents ..................................................................................... 4

1.5 Loading the Content of a Document ............................................................. 5

Page 4: Web Services Code Examples Web Services Code Examples In the following chapters some code examples explain how to use the SAPERION web service by using the IDE Visual Studio 2008 and

2

Web Services Code Examples

In the following chapters some code examples explain how to use the SAPERION web service by using

the IDE Visual Studio 2008 and the C# programming language.

1 Login Example

The login example uses the AuthenticationService at the endpoint

‘<URL>/scr-webservices/soap/AuthenticationService’ and the WSDL file

‘<URL>/scr-webservices/soap/AuthenticationService?wsdl’. Visual Studio automatically generates all

necessary stubs to call the web service.

1.1 Creating AuthenticationService Stubs

To create authentication service stub follow the described steps:

1. Open your Visual Studio project make a right click on ‘Web References’ in the Solution Explorer.

2. Select the entry "Add Web Reference". In the appearing dialog enter the URL of the

AuthenticationService’s WSDL file.

Fig. 1–1: Dialog "Add Web Reference"

3. Click on [Go] and subsequently enter a web reference name.

4. Click on [Add Reference]. Visual Studio now generates all necessary classes to perform

AuthenticationService web service calls.

1.2 Logging in a User

The following sample code illustrated how to login a user via the SAPERION web services.

Page 5: Web Services Code Examples Web Services Code Examples In the following chapters some code examples explain how to use the SAPERION web service by using the IDE Visual Studio 2008 and

1 Login Example 3

using System;

using System.Collections.Generic;

using System.Text;

namespace webService_NUnits.Guide

{

class ExampleUserLogin

{

/* Logs in the user with the given values against the SAPERION AuthenticationService. */

public int loginUser(String username, String password, int licenseType, String mandant)

{

try

{

AuthService.SaWsAuthenticationServiceService service = new

AuthService.SaWsAuthenticationServiceService();

int token = service.login(username, password, licenseType, mandant);

System.Diagnostics.Trace.WriteLine("Logged in user: " + username + ". Retrieved token: " + token);

//return token for further usage.

return token;

}

catch (System.Exception ex)

{

System.Diagnostics.Trace.WriteLine("Exception while trying to login: " + ex.Message);

return 0;

}

}

}

}

1.3 Archive a Document

To archive a document it is necessary to use the ArchiveService. Please perform the

steps described in chapter "Creating AuthenticationService Stubs" above with the WSDL URL

‘<URL>/scr-webservices/soap/ArchiveService?wsdl’ to add a ArchiveService web reference to your Visual

Studio project. The web reference name could be something like "ArchiveService".

The following code shows how to archive an example document:

using System;

using System.Collections.Generic;

using System.Text;

namespace webService_NUnits.Guide

{

using webService_NUnits.ArchiveService;

class ExampleArchivation

{

Page 6: Web Services Code Examples Web Services Code Examples In the following chapters some code examples explain how to use the SAPERION web service by using the IDE Visual Studio 2008 and

4

public String archive(int token, String ddc, String content, String comment)

{

//instantiate service

SaWsArchiveServiceService archiveService = new SaWsArchiveServiceService();

//document meta data

saWsProperty[] metadata = new saWsProperty[1];

saWsProperty metaOne = new saWsProperty();

metaOne.name = "name1";

metaOne.value = "value1";

metadata[0] = metaOne;

//contents

saWsContent[] contents = new saWsContent[0];

saWsContent contentStream = new saWsContent();

contentStream.content = Encoding.ASCII.GetBytes(content);

contentStream.filename = "test.txt";

contents[0] = contentStream;

saWsContentContainer container = new saWsContentContainer();

container.content = contents;

container.mode = mode.REPLACE;

//perform webservice call

saWsSaveInfo saveInfo = archiveService.create(token, ddc, metadata, container, comment, "");

return saveInfo.HDoc;

}

}

}

As you can see, document meta data are provided by a saWsProperty array (assuming that the given

DDC has a meta data value by the name of "name1") and the contents as a saWsContentContainer.

In this example the given content string is converted to a byte-array and the according file name is set

to "test.txt". The last parameter of the create method is an empty string indicating that the created

document is not restricted by an ACL. Otherwise the last parameter would state the name of an ACL,

which restricts the created document.

1.4 Searching documents

The document search will be also processed via the ArchiveService, therefore no additinal web reference

needs to be generated. The following code depicts how to search documents via the SAPERION web

services.

using System;

using System.Collections.Generic;

using System.Text;

namespace webService_NUnits.Guide

{

Page 7: Web Services Code Examples Web Services Code Examples In the following chapters some code examples explain how to use the SAPERION web service by using the IDE Visual Studio 2008 and

1 Login Example 5

using webService_NUnits.ArchiveService;

class ExampleSearch

{

public saWsPropertyValueArray[] searchDocuments(int token, String ddc)

{

//instantiate service

SaWsArchiveServiceService archiveService = new

SaWsArchiveServiceService();

//prepare HQL query

String hql = " from " + ddc;

//perform search

saWsPropertyValueArray[] results = archiveService.search(token, hql);

System.Diagnostics.Trace.WriteLine("Searched " + results.Length + "

documents with the query: " + hql);

return results;

}

}

}

In this example all documents by the given DDC are searched and returned as an array of

saWsPropertyValueArray. As you can see searching is done by stating a valid HQL query.

1.5 Loading the Content of a Document

Loading the content of a document is processed via the ContentService. Therefore you need to perform

the steps described in chapter "Creating AuthenticationService Stubs" above with the WSDL URL

‘<URL>/scr-webservices/soap/ContentService?wsdl’ to add a ContentService web reference to your

Visual Studio project. The web reference name could be something like "ContentService".

The following example shows how to load the content of a specific document.

using System;

using System.Collections.Generic;

using System.Text;

namespace webService_NUnits.Guide

{

using webService_NUnits.ContentService;

class ExampleContentRetrieval

{

public saWsContent getContent(int token, String hdoc, int elementNumber)

{

//instantiate service

SaWsContentServiceService contentService = new

SaWsContentServiceService();

Page 8: Web Services Code Examples Web Services Code Examples In the following chapters some code examples explain how to use the SAPERION web service by using the IDE Visual Studio 2008 and

6

//get content

saWsContent content = contentService.getContent(token, hdoc,

elementNumber);

System.Diagnostics.Trace.WriteLine("Retrieved content number " +

elementNumber + " of document " + hdoc + ". Filename: " +

content.filename);

//return content

return content;

}

}

}

As you can see, retrieving a document content is performed by passing the documents ID (hdoc) and the

element number of the structured document. The retrieved saWsContent object contains the filename

and a byte[] representing the content itself.