次の方法で共有


Connecting to SharePoint Online Web Services

Last week we released Remote Authentication in SharePoint Online Using Claims Based Authentication, which, along with its associated code sample, not only demonstrates how to authenticate against SharePoint Online in client applications using the SharePoint client-side object models, but also provides developers with a set of classes that they can use to perform federated user authentication for SharePoint Online.

One related question that, while out of scope for the article and code sample, is likely to be of interest to developers is: how do I authenticate and connect to SharePoint Online Web services, such as list.asmx or web.asmx?

Manish Joshi, one of our talented support engineers here in Office, was kind enough to augment the code presented in the Remote Authentication article to demonstrate one approach to using Web services in SharePoint Online. In the example below, he edits the console application from the code sample so that it accesses the lists.asmx service and prints out a list of items from the Shared Documents library of the target site. If you’re a developer looking to access the Web services available in SharePoint Online, this should get you started.

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Client;
using System.Net;
using MSDN.Samples.ClaimsAuth;
 namespace Sp_Ctx
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            //if (args.Length < 1) { Console.WriteLine("SP_Ctx <url>"); return; }
            //string targetSite = args[0];
            string targetSite = "https://sharepointOnlineURLRoot/sites/siteName";
            using (ClientContext ctx = ClaimClientContext.GetAuthenticatedContext(targetSite))
            {
                if (ctx != null)
                {
                    ctx.Load(ctx.Web); // Query for Web
                    ctx.ExecuteQuery(); // Execute
                    Console.WriteLine(ctx.Web.Title);
                }  
            }
            Console.WriteLine("");
            Console.WriteLine("");
            CookieCollection authCookie = 
                ClaimClientContext.GetAuthenticatedCookies(targetSite, 925, 525);
            listWS.Lists list = new listWS.Lists();
            list.Url = "https://sharepointOnlineURLRoot/sites/siteName/_vti_bin/Lists.asmx"
            //list.Timeout = 15000; //in milliseconds
            list.CookieContainer = new CookieContainer();
            list.CookieContainer.Add(authCookie);
            string listName = "Shared Documents";
            string viewName = "";
            //string listName = "{1A4A3C5D-360E-45EB-B9ED-E8653981CAC0}";
            //string viewName = "{5A4AF2C5-8A9F-427F-B8AA-BC59E3BE8AA0}";
            string rowLimit = "5";
            // Instantiate an XmlDocument object         
            System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
            System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
            System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
            System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
            
            //*Use CAML query*/        
            query.InnerXml = "<Where><Gt><FieldRef Name=\"ID\" />" + 
                "<Value Type=\"Counter\">0</Value></Gt></Where>";
            viewFields.InnerXml = "<FieldRef Name=\"Title\" />";
            //queryOptions.InnerXml = "";
            queryOptions.InnerXml = 
                "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns>" + 
                "<DateInUtc>TRUE</DateInUtc>";
            System.Xml.XmlNode nodes = 
                list.GetListItems(
                    listName, 
                    viewName, 
                    query, 
                    viewFields, 
                    rowLimit, 
                    null, 
                    string.Empty);
            string ixml = list.GetList(listName).InnerXml;
            Console.WriteLine(
                "Retrieving title of all the items in SharePoint Online" +
                 "sites 'Shared Documents' using Lists webservice");
            Console.WriteLine(
                "===========================================" +
                "=============================================");
            foreach (System.Xml.XmlNode node in nodes)
            {
                if (node.Name == "rs:data")
                {
                    for (int i = 0; i < node.ChildNodes.Count; i++)
                    {
                        if (node.ChildNodes[i].Name == "z:row")
                        {
                            Console.WriteLine(
                                node.ChildNodes[i].Attributes["ows_Title"].Value);
                        }
                    }
                }
            }
            Console.ReadLine();
        }
    }
}

Comments

  • Anonymous
    May 12, 2011
    All well and good for an 'interactive process' but what about a worker role (in Azure) trying to communicate back SharePoint Online ? How does one go about that (I take it so far we can't authenticate directly without user interaction) ?

  • Anonymous
    May 13, 2011
    Adrian:  Please have a read of this: blogs.msdn.com/.../part-2-headless-authentication-with-sharepoint-online-and-the-client-side-object-model.aspx

  • Anonymous
    July 10, 2011
    What is  "listWS" I'm getting an error on that

  • Anonymous
    September 22, 2011
    I'm also getting an error on listWS. I assume this is an object to encapsulates a web service, but I don't know how to add this to the project.

  • Anonymous
    October 12, 2011
    Hello, does anyone knows what to do to solve the issue with listWS? I really need to test the code but I do not know how to proceed here... Thanks

  • Anonymous
    January 17, 2012
    I guess listWS is just standard Web Service Proxy class generated by Visual Studio for ///Lists.asmx web service.

  • Anonymous
    September 18, 2012
    Does anyone know if/how I can use a similar technique to authenticate and connect to SPO web services in PowerShell? I'm using New-WebServiceProxy but am getting "access denied"... I'm not a programmer so am not sure which parts of this I would take into PowerShell (or how) to get authenticated. Thanks!

  • Anonymous
    September 29, 2013
    listWS This is actually proxy instance of the your List webservice.

  • Anonymous
    November 18, 2013
    What if we don't want to use Login window? Just provide credentials via code? Is there any way to achieve this ? I am not able to do this in Office365 when it is in custom domain.