简体   繁体   English

使用Web服务遍历Sharepoint列表项的属性,得到“对象引用未设置为对象的实例”

[英]Iterating through attributes of Sharepoint list items using web service, getting “Object reference not set to an instance of an object”

I'm using C# to call Sharepoint web services. 我正在使用C#调用Sharepoint Web服务。 I want to get a Sharepoint list, and then for each item in the list, write its attributes to the console. 我想要一个Sharepoint列表,然后为列表中的每个项目将其属性写入控制台。 I keep getting an "Object reference not set to an instance of an object" when I iterate through the attributes with foreach . 当我使用foreach遍历属性时,我不断收到“对象引用未设置为对象的实例”的信息。 Code as follows: 代码如下:

class Program
    {
        static void Main(string[] args)
        {
            try
            {                 
                cSharpTest_service.Lists lists = new cSharpTest_service.Lists();
                lists.Url = "http://wsssandbox/sites/cSharp/_vti_bin/lists.asmx";
                lists.Credentials = System.Net.CredentialCache.DefaultCredentials;
                System.Xml.XmlNode listData = lists.GetListItems("cSharpTestList", "", null, null, "", null, "");



                foreach (System.Xml.XmlNode iterateNode in listData)
                {

                    Console.WriteLine("--NODE--");
                    System.Xml.XmlAttributeCollection attrs = iterateNode.Attributes;
                    foreach (System.Xml.XmlAttribute attr in attrs)
                    {
                        Console.WriteLine("Name:");
                        Console.WriteLine(attr.Name);
                        Console.WriteLine("Value:");
                        Console.WriteLine(attr.Value);
                    }

                }
                Console.ReadLine();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.ReadLine();
            }


        }
      }

I think it's throwing the exception because some of the attributes are Null for some of the list items. 我认为这引发了异常,因为某些列表项的某些属性为Null But I can't figure out how to check whether or not the attribute is null as part of the foreach iteration. 但是我不知道如何在foreach迭代中检查该属性是否为null。

Take a look at this example and this article . 看一下这个例子和这篇文章

public void getListData()
{
  WS_Lists.Lists myservice = new WS_Lists.Lists();
  myservice.Credentials = System.Net.CredentialCache.DefaultCredentials;
  myservice.Url = "http://merdev-moss:5050/testsara/_vti_bin/Lists.asmx";

  try
  {
    /* Assign values to pass the GetListItems method*/
    string listName = "{5C65CB1A-2E1B-488A-AC07-B115CD0FC647}";
    string viewName = "{75E689B4-5773-43CB-8324-58E42E1EB885}";
    string rowLimit = "100";

    // 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 = "";

    System.Xml.XmlNode nodes = myservice.GetListItems(listName, viewName, query, viewFields, rowLimit, null, null);

    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")
                {
                    Response.Write(node.ChildNodes[i].Attributes["ows_Title"].Value + "</br>");
                }
            }
        }
    }
}
 catch (Exception ex)
 {
    Response.Write(ex.Message);
 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Web服务将文件上载到Sharepoint 2013 - 未将对象引用设置为对象的实例 - Upload file to Sharepoint 2013 using web service - Object reference not set to an instance of an object blazor web 装配服务 object 引用未设置为 object 的实例 - blazor web assembly service object reference not set to an instance of an object 使用Logging Service时,对象引用未设置为对象的实例 - Object reference not set to an instance of an object when using Logging Service 未使用linq将对象引用设置为对象列表的实例 - Object reference not set to an instance of an object list to a list using linq 使用任务获取“对象引用未设置为对象的实例” - Using tasks getting “Object reference not set to an instance of an object” 未将对象设置为引用Web服务 - Object not set to reference with web service 获取错误对象引用未设置为对象的实例 - Getting error Object reference not set to an instance of an object 获取错误“对象引用未设置为对象的实例” - Getting error “Object reference not set to an instance of an object” 在C#中遍历DataGridView-对象引用未设置为对象的实例 - iterating over a DataGridView in C# - Object reference not set to an instance of an object 我在共享点列表中添加了一个新的splistitem,但它总是给我一个例外:“对象引用未设置为对象的实例” - I am adding a new splistitem to a sharepoint list but it always gives me an exception: “object reference not set to an instance of an object”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM