简体   繁体   中英

How to connect Office 365 - SharePoint edition site using a external Rest web service made without using SharePoint?

I am totally new to SharePoint. I have a Rest Web service which is not made using SharePoint. Can I connect our SharePoint site(Office 365 - SharePoint edition) using this Rest web service(if yes, then how can I implement it) or do I need to make a rest service using SharePoint?

I will be very thankful if anybody can help me out. Thanks in advance.

There are the HttpWebRequest and HttpWebResponse classes that allow you to call REST services. I have used it before to tap into services like Parse.com (where I could not use the native C# interface) as well as my own web services. Here is an example from MSDN:

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);

        // Set some reasonable limits on resources used by this request
        request.MaximumAutomaticRedirections = 4;
        request.MaximumResponseHeadersLength = 4;
        // Set credentials to use for this request.
        request.Credentials = CredentialCache.DefaultCredentials;
        HttpWebResponse response = (HttpWebResponse)request.GetResponse ();

        Console.WriteLine ("Content length is {0}", response.ContentLength);
        Console.WriteLine ("Content type is {0}", response.ContentType);

        // Get the stream associated with the response.
        Stream receiveStream = response.GetResponseStream ();

        // Pipes the stream to a higher level stream reader with the required encoding format. 
        StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);

        Console.WriteLine ("Response stream received.");
        Console.WriteLine (readStream.ReadToEnd ());
        response.Close ();
        readStream.Close ();

You can find more here: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx

Here is an example of a piece of code I use to obtain friends for a user:

        public static WebExceptionStatus processResponse(HttpWebRequest response, out JsonValue values)
        {
            try
            {
                using (WebResponse stream = response.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(stream.GetResponseStream()))
                    {
                        values = JsonObject.Parse (reader.ReadToEnd ());
                        return WebExceptionStatus.Success;
                    }
                }
            }
            catch (WebException exception) {
                values = null;
                return exception.Status;
            }
        }

...

        HttpWebRequest request = HttpWebRequest.Create ("myURL") as HttpWebRequest;
        request.Headers["SOMEH_HAEDER_VALUE"] = applicationID;
        request.Method = "GET";
        request.ContentType = "application/json";

        JsonValue values;
        status = RequestDriver.processResponse (request, out values);

        if (RequestDriver.HadException (status)) {
            ExceptionHandler.handleNetworkException(status);
        }

        if (values ["results"].Count == 0) {
            return null;
        }

        JsonValue userValue = values ["results"] [0];

        string name = RequestDriver.stripQuotes( userValue["Name"].ToString() );
        string objectId = RequestDriver.stripQuotes( userValue["objectId"].ToString() );

        User user = new User (name, email, objectId);
        return user;

You can use the REST service in SharePoint to access data like list and list items. http://msdn.microsoft.com/library/office/dn292552.aspx

You will need to get an access token to add to the Authorization header in your request. To get a token for your request you must register your app in Azure Active Directory. This article explains how to do that http://msdn.microsoft.com/en-us/library/office/dn605894(v=office.15).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM