views:

260

answers:

1

Is there any MOSS out of the box web service which takes the URL of a SharePoint site and tells us if that URL is pointing to a file (document)? For example, we have a list of SharePoint URLs and we need to find out which URLs are pointing to a file and not a document library or a list?

A: 

I would seriously consider going with propfind (webdav). I can't give you an exact answer on how to check for file or document library or list, but I can post a function that can serve as a base for something you might implement... here it is....

/// <summary>
        /// Checks if MOSS resource exists.
        /// </summary>
        /// <param name="url">
        /// The url to the resource.
        /// </param>
        /// <returns>
        /// True or false.
        /// </returns>
        /// <exception cref="Exception">
        /// </exception>
        private bool MossResourceExists(string url)
        {
            // Create the web request object
            var oReq = (HttpWebRequest) WebRequest.Create(url);

            // Set the needed properties
            oReq.Method = "PROPFIND";
            oReq.Credentials = wsLists.Credentials; // Use same credentials as wsLists. 
            oReq.AllowAutoRedirect = true;
            oReq.UserAgent = "Microsoft-WebDAV-MiniRedir/6.1.7600";

            try
            {
                // Enumerate through top level only, increasing the depth will find children.
                oReq.Headers["Depth"] = "0";
                oReq.Headers["translate"] = "f";
                var oRequest = new StreamWriter(oReq.GetRequestStream());
                oRequest.WriteLine();
                oRequest.Close();
                oReq.GetResponse();
                ////done with the webclient stuff, check the results

                //var oMyDoc = new XmlDocument();
                //oMyDoc.LoadXml(sResponse);
                //var oNsMgr = new XmlNamespaceManager(oMyDoc.NameTable);
                //oNsMgr.AddNamespace("D", "DAV:");

                //XmlNodeList oAllResponses = oMyDoc.SelectNodes(".//D:multistatus/D:response", oNsMgr);

                //foreach (XmlNode oNode in oAllResponses)
                //{
                //    if ()
                //    string oNodeURL = oNode.SelectSingleNode("./D:href", oNsMgr).InnerText.ToLower()
                //    Console.WriteLine("Name: " + 
                //                      oNode.SelectSingleNode("./D:propstat/D:prop/D:displayname",
                //                      oNsMgr).InnerText);

                //    if (oNode.SelectNodes("./D:propstat/D:prop/D:isFolder", oNsMgr).Count > 0)
                //    {
                //        Console.WriteLine("Is folder: " +
                //                oNode.SelectSingleNode("./D:propstat/D:prop/D:isFolder",
                //                oNsMgr).InnerText);
                //    }
                //    else
                //    {
                //        Console.WriteLine("Is folder: f");
                //    }
                //    Console.WriteLine();
                //}
            }
            catch (WebException ex)
            {
                var errorResponse = ex.Response as HttpWebResponse;

                if (errorResponse != null)
                    if (errorResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        return false;
                    }
                    else
                    {
                        throw new Exception("Error checking if URL exists:" + url + ";Status Code:" +
                                            errorResponse.StatusCode + ";Error Message:" + ex.Message);
                    }
            }
            return true;
}
JL