MSDN 的一点小误解(创建文件夹出错)

[关键字]:SPS 2007 ,Web Service,Create Folder,The parent folder does not exist,FolderNotFound

[问题描述] http://msdn2.microsoft.com/en-us/library/websvcdocumentworkspace.dws.createfolder.aspx ,这篇msdn的文章介绍了如何调用SPS 2007的一个Web Services 函数CreateFolder 来创建一个文件夹到指定的站点目录。

比如我们有两个site,url 分别是http://spsServerName/default.aspx(根站点) , http://spsServerName/sites/mytestsite/default.aspx (子站点).

利用如下的代码(来源于msdn)可以建立一个文件夹到http://spsServerName/Shared Documents.

//首先添加 web reference http://spsServerName/_vti_bin/dws.asmx
try
{
    
string strResult = "";
    strResult 
= 
        dwsWebService.CreateFolder(
"Shared Documents/folder_name");
    
if (IsDwsErrorResult(strResult))
    
{
        
int intErrorID  = 0;
        
string strErrorMsg = "";
        ParseDwsErrorResult(strResult, 
out intErrorID, out strErrorMsg);
        MessageBox.Show
            (
"A document workspace error occurred.\r\n" +
            
"Error number: " + intErrorID.ToString() + "\r\n" +
            
"Error description: " + strErrorMsg,
            
"DWS Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    
else
    
{
        MessageBox.Show
            (
"The folder was successfully created.",
            
"Create Folder", MessageBoxButtons.OK,
            MessageBoxIcon.Information);
    }

}

catch (Exception exc)
{
    MessageBox.Show(
"An exception occurred.\r\n" +
        
"Description: " + exc.Message,
        
"Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}


但是如果想创建文件夹到http://spsServerName/sites/mytestsite/Shared Documents ,通过

dwsWebService.CreateFolder("sites/mytestsite/Shared Documents/folder_name")

或者

dwsWebService.CreateFolder(
"http://spsServerName/sites/mytestsite/Shared Documents/folder_name")

就会得到如下错误信息:

FolderNotFound (10). The parent folder does not exist.

所以通过CreateFolder来指定路径是不对的。如果重新添加子site 的web reference  http://spsServerName/sites/mytestsite/_vti_bin/dws.asmx  也无法创建文件夹到当前的这个子site "mytestsite"的 “ Shared Documents”下

[原因&解决办法]

如果你查看一下工程文件里的Reference.cs 这个文件,你就会发现真正的原因。你会看到如下的代码:

public Dws() {
            
this.Url = global::ConsoleApplication1.Properties.Settings.Default.ConsoleApplication1_localhost_Dws;
            
if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
                
this.UseDefaultCredentials = true;
                
this.useDefaultCredentialsSetExplicitly = false;
            }

            
else {
                
this.useDefaultCredentialsSetExplicitly = true;
            }

        }
哈哈,这就是为什么总只能在root site 下创建文件夹,而不能在下面的site下创建文件夹了吧?就算我们添加子site的web reference也会被指向根site 的.
所以我们只需要在上面的MSDN代码前添加一句就可以了
 dwsWebService.Url = "http://spsServerName/sites/mytestsite/_vti_bin/dws.asmx";

完整代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using ConsoleApplication1.localhost;
using System.IO;
namespace ConsoleApplication1
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
try
            
{
                
string strResult = "";
                
// create a web reference to http://spsServerName/_vti_bin/dws.asmx"
                localhost.Dws dwsWebService = new ConsoleApplication1.localhost.Dws();
                
//if you want to create fold to http://spsServerName/sites/mytestsite , please set dwsWebService.Url manually.
                dwsWebService.Url = "http://spsServerName/sites/mytestsite/_vti_bin/dws.asmx";
                dwsWebService.Credentials 
= System.Net.CredentialCache.DefaultNetworkCredentials;
               
                strResult
=
                    dwsWebService.CreateFolder(
"Shared Documents/myfolder");
               
// Console.WriteLine(dwsWebService.Url);
                if (IsDwsErrorResult(strResult))
                
{
                    
int intErrorID = 0;
                    
string strErrorMsg = "";
                    ParseDwsErrorResult(strResult, 
out intErrorID, out strErrorMsg);
                    MessageBox.Show
                        (
"A document workspace error occurred.\r\n" +
                        
"Error number: " + intErrorID.ToString() + "\r\n" +
                        
"Error description: " + strErrorMsg,
                        
"DWS Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                
else
                
{
                    MessageBox.Show
                        (
"The folder was successfully created.",
                        
"Create Folder", MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
                }

            }

            
catch (Exception exc)
            
{
                MessageBox.Show(
"An exception occurred.\r\n" +
                    
"Description: " + exc.Message,
                    
"Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            
        }

        
static bool IsDwsErrorResult(string ResultFragment)
        
{
            System.IO.StringReader srResult 
= new System.IO.StringReader(ResultFragment);
            System.Xml.XmlTextReader xtr 
= new System.Xml.XmlTextReader(srResult);
            xtr.Read();
            
if (xtr.Name == "Error")
            
{
                
return true;
            }

            
else
            
{
                
return false;
            }

        }


        
static void ParseDwsErrorResult(string ErrorFragment, out int ErrorID, out string ErrorMsg)
        
{
            System.IO.StringReader srError 
= new System.IO.StringReader(ErrorFragment);
            System.Xml.XmlTextReader xtr 
= new System.Xml.XmlTextReader(srError);
            xtr.Read();
            xtr.MoveToAttribute(
"ID");
            xtr.ReadAttributeValue();
            ErrorID 
= System.Convert.ToInt32(xtr.Value);
            ErrorMsg 
= xtr.ReadString();
        }



    }

}



//后记:这个问题其实不是很难,但是如果不仔细看的话,确实想不到原因,总是想当然的以为在函数CreateFolder 中设置路径。在我解决这个问题后,就特别想把它赶快写下来,以供大家参考,以免碰到同样的问题多走弯路,尤其是这个问题是在网上搜不到,如果刚好有人遇到这个问题可以参考一下。
posted @ 2006-11-01 17:23  bluehat  阅读(1320)  评论(0编辑  收藏  举报