【问题标题】:SharePoint: How to create a folder in a document library please using web servicesSharePoint:如何在文档库中创建文件夹,请使用 Web 服务
【发布时间】:2010-12-10 11:46:50
【问题描述】:

我需要在 SharePoint 的文档库中创建一个简单的文件夹,但我似乎找不到有关该主题的文档片段。

dws webservice 好像是用来在工作空间中创建物理文件夹的,我需要一种在文档库中创建文件夹的方法。

不知道怎么办,请帮忙

【问题讨论】:

  • 这些答案是否根据需要创建父目录,即如果使用/one/two 调用它们会创建/one/one/two 吗?

标签: sharepoint-2007 wss


【解决方案1】:

我发现这个方法可行:

    HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create("http://mySite/MyList/MyfolderIwantedtocreate");
    request.Credentials = CredentialCache.DefaultCredentials;
    request.Method = "MKCOL";
    HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
    response.Close();

【讨论】:

    【解决方案2】:

    这是在 JAVA 中使用 apache HttpClient 进行类似请求的代码

    import org.apache.http.*;
    
    private static HttpResponse makeFolder(
                String url,
                DefaultHttpClient httpClient) throws Exception {
        BasicHttpRequest httpPost = new BasicHttpRequest("MKCOL", url);
        HttpUriRequest httpUriRequest = new RequestWrapper(httpPost);
    
        HttpResponse status = httpClient.execute(httpUriRequest);
        EntityUtils.consume(status.getEntity());
        return status;
    }
    

    创建 httpClient 并调用 makeFolder 函数的代码

    DefaultHttpClient httpClient = new DefaultHttpClient();
    httpClient.getCredentialsProvider().setCredentials(
            AuthScope.ANY,
            new NTCredentials(config.getUserName(), config.getPasswords(),
                            "", config.getDomain()));
    

    【讨论】:

      【解决方案3】:

      我知道这是一个很老的问题,但如果其他人发现它,我就是这样做的:

             String CAML =  "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
              "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                  "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " +
                  "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
              "<soap:Body>" +
              "<CreateFolder " + "xmlns=\"http://schemas.microsoft.com/sharepoint/soap/dws/\">"+
                  "<url>" + ParentFolder+'/'+NewFolderName+ "</url>"+
              "</CreateFolder>"+
              "</soap:Body>" +
              "</soap:Envelope>";
      
             String uri = "http://[your site]/_vti_bin/dws.asmx";
      
             WebClient client = new WebClient();
              client.Headers["SOAPAction"] = "http://schemas.microsoft.com/sharepoint/soap/dws/CreateFolder";
              client.Headers["content-type"] = "text/xml; charset=utf-8";
              client.Encoding = Encoding.UTF8;
              client.UploadStringCompleted += UploadStringCompleted;
              try
              {
                  client.UploadStringAsync(new Uri(uri, UriKind.Absolute), "POST", CAML);
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Error in upload string async: " + ex.Message);
              }
      

      我使用的是silverlight,这就是我使用上传字符串异步的原因,但这可以通过相同的http post方法以其他方式完成

      【讨论】:

      【解决方案4】:

      我已经使用 Web 服务完成了一些工作,但我找不到任何创建文件夹的代码。但是,我的代码使用 UNC 路径将文件从网络共享复制到 SharePoint 文档库中的现有文件夹。它使用 System.IO.File - 也许您可以使用该技术来创建文件夹?

      【讨论】:

        【解决方案5】:

        使用Document Workspace Web Service (Dws) 在sharepoint 中创建文件夹。效果很好。

        public static bool CreateSPFolder(string FolderDir, string FolderName, yourCredentialsClass credentials)
        {
            FolderName = ReplaceInvalidChars(FolderName);
        
            // create an instance of the sharepoint service reference
            Dws.Dws dwsWebService = new Dws.Dws();
            dwsWebService.Url = credentials.SharePointUrl + "/_vti_bin/Dws.asmx";
            dwsWebService.Credentials = new NetworkCredential(credentials.UserId, credentials.Password);
        
            string result = dwsWebService.CreateFolder(string.Format("{0}/{1}",FolderDir,FolderName));
            dwsWebService.Dispose();
        
            if (result == null)
            {
                throw new Exception("No response creating SharePoint folder");
            }
        
            if (result.Equals("<Result/>"))
            {
                return true;
            }
        
            return false;
        }
        

        public static string ReplaceInvalidChars(string strIn)
        {
            return Regex.Replace(strIn.Replace('"', '-'), @"[.~#%&*{}:<>?|/]", "-");
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-21
          • 2010-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-22
          • 1970-01-01
          相关资源
          最近更新 更多