How to create a folder in WSS site(with httpWebRequest)  
Author Message
WSS22WSS3





PostPosted: Visual C# General, How to create a folder in WSS site(with httpWebRequest) Top

I just want to create a folder(say a document library) in WSS site, and I have codes below, they don't work yet, always raise an exception says: 404 not found. Can somebody be kind to help me out Thank you guys in advance.

public static void CreateDir_WebRequest() //under working...

{

string actionUrl = "http://localhost/_vti_bin/owssvr.dll CS=109";

string referUrl =

"http://localhost/_layouts/1033/new.aspx " +

"ListTemplate=101&ListBaseType=1";

string boundary = "----------" + DateTime.Now.Ticks.ToString("x");

HttpWebRequest preRequest =

(HttpWebRequest)HttpWebRequest.Create(referUrl);

preRequest.UseDefaultCredentials = true;

preRequest.KeepAlive = true;

HttpWebResponse preResponse =

(HttpWebResponse)preRequest.GetResponse();

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(actionUrl);

request.Credentials = CredentialCache.DefaultCredentials;

request.ContentType = "Application/x-www-form-urlencoded" + boundary;

request.Method = "POST";

request.Referer = referUrl;

request.Accept = "*/*";

request.KeepAlive = true;

request.AllowAutoRedirect = true;

for (int i = 0; i < preResponse.Cookies.Count; i++)

{

request.CookieContainer.Add(preResponse.CookiesIdea);

}

string requestString = null;

requestString += HttpUtility.UrlEncode("--" + boundary);

requestString += HttpUtility.UrlEncode("Title=\"CCC\"&");

requestString += HttpUtility.UrlEncode("Description=\"CCC\"&");

requestString += HttpUtility.UrlEncode("displayOnLeft=\"TRUE\"&");

requestString += HttpUtility.UrlEncode("VersioningEnabled=\"FALSE\"&");

requestString += HttpUtility.UrlEncode("DocumentTemplateType=101&");

requestString += HttpUtility.UrlEncode("ListTemplate=1&");

requestString += HttpUtility.UrlEncode("Project=\"http://localhost\"&");

requestString += HttpUtility.UrlEncode("Cmd=\"NewList\"");

byte[] data = Encoding.ASCII.GetBytes(requestString);

request.ContentLength = data.Length;

Stream writer = request.GetRequestStream();

writer.Write(data, 0, data.Length);

writer.Flush();

writer.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

string responseString = null;

StreamReader reader = new StreamReader(response.GetResponseStream());

responseString = reader.ReadToEnd();

SaveStringToFile(responseString, "c:\\test\\CreateDir_WebRequest.txt");

}



Visual C#17  
 
 
Keith Rome





PostPosted: Visual C# General, How to create a folder in WSS site(with httpWebRequest) Top

Judging from your code sample, it looks like you are just wanting to create this on a WSS site that is running on the local server (same server as this code is running)

If you are wanting to create a WSS document library (or any other WSS object) on the local server, then your best bet is to just leverage the SharePoint Object Model libraries. Just add a reference to the sharepoint DLL (Microsoft.SharePoint.dll), and then you can access the full range of WSS capabilities.

Please allow me to direct you to a (free) sharepoint developer training resource that I helped create last year: www.sharepoint123.com

If you follow the link there to download the content, you will see level 100, 200, and 300 presentations + Hands On Lab downloads. You will probably be most interested in the Level 300 download, there is a PDF manual for the entire hands-on lab session. Lesson 1 / Exercise 2 in that lab manual is pretty much a walkthrough of how to build an external program that manipulates WSS sites. You can work with Document Libraries in much the same way. Also, this sample is written as a Windows Forms application, but you could just as easily have written it as a Web application.

HTH



 
 
WSS22WSS3





PostPosted: Visual C# General, How to create a folder in WSS site(with httpWebRequest) Top

Thanks Keith much.

The program will run in a dist enviroment. That's why i use Http request but not SDK of WSS. Thank you any way, the training resource is good.


 
 
Keith Rome





PostPosted: Visual C# General, How to create a folder in WSS site(with httpWebRequest) Top

The SOM should still work even for a distributed WSS farm. All of this stuff is just stored in the WSS content database. The SOM just makes it a little easier to get to.

HTH



 
 
WSS22WSS3





PostPosted: Visual C# General, How to create a folder in WSS site(with httpWebRequest) Top

OK, I will try it and let you know. Thank you.
 
 
WSS22WSS3





PostPosted: Visual C# General, How to create a folder in WSS site(with httpWebRequest) Top

Moss work in distributed environment.But the codes should located at one of the machine which host the farm.

I finally walk around it through web service.

Thanks for your kind help.