How to obtain the ID of a newly uploaded document using web services (c#, .net)? Is the following the best way to obtain Doc ID?

private string sGetID(string sURL, string sListGUID, string sFileName)
    string sUser = ConfigurationManager.AppSettings("User");
    string sPwd = ConfigurationManager.AppSettings("Pwd");
    string sDomain = ConfigurationManager.AppSettings("Domain");
    System.Net.NetworkCredential netAccess = new System.Net.NetworkCredential(sUser, sPwd, sDomain);
    SPLists.Lists L = new SPLists.Lists();
    L.Credentials = netAccess;
    L.Url = sURL;
    XmlDocument xmldoc = new XmlDocument();
    XmlNode query = xmldoc.CreateNode(XmlNodeType.Element, "Query", "");
    query.InnerXml = "<OrderBy><FieldRef Name='Modified' " + "Ascending='False'></FieldRef></OrderBy>\"";
    try {
        XmlNode caml = L.GetListItems(sListGUID, null, query, null, "1", null);
        string id = caml.ChildNodes(1).ChildNodes(1).Attributes("ows_ID").Value;
        return id;
    } catch (Exception ex) {
        return ex.Message;
  • 1
    I think that you should use "Created" field instead of "Modified", in your solution you will get items ordered by update time.
    – Alexander
    Oct 24 '11 at 19:51

_dlc_DocId is the internal field that has allows you to change or retrive the document id. also agree with row limit. but if a newly created/uploaded document as in version 1. then maybe insted of modified you can use created

kind regards


I'd add a RowLimit node to the CAML Query, just to be sure to limit the result set to 1, ensures that your web services request brings back only the data it needs (imagine if you had thousands of items in that library!).

  • How to use SiteData.GetURLSegments method? Oct 24 '11 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.