Tuesday, March 24, 2015

Rename or Move a SharePoint list along with content using Http request (Without SP server side code)



The option to rename or Move a SharePoint list along with content is not available with SharePoint Client object model (CSOM) and the same can be achieved using the http request to SharePoint. Refer the code below.


string sourceUrl = "https://contoso.sharepoint.com/teams/site1/lists/List1";

string targetUrl = "https://contoso.sharepoint.com/teams/site1/lists/List1-move";

// Create the HttpWebRequest object.

HttpWebRequest request = (System.Net.HttpWebRequest)HttpWebRequest.Create(sourceUrl);


 
// FOR SHarePoint online- Set fedauth Authentication Cookie

if (IsSPOSite(sourceUrl)) {

SecureString securestring = new SecureString();

foreach (char c in "password".ToCharArray()) securestring.AppendChar(c);

SharePointOnlineCredentials SPOcredentials = new SharePointOnlineCredentials("someone@mycompany.com", securestring);

// Get the auth cookie

string fedAuthCookie = SPOcredentials.GetAuthenticationCookie(new Uri("https://contoso.sharepoint.com"));

request.Headers.Add("Cookie", fedAuthCookie); }

// on-premise

else {

request.UseDefaultCredentials = true; }


 
 
// Specify the method.

request.Method = "MOVE";

// Specify the destination URI.

request.Headers.Add("Destination", targetUrl );

// if a resource already exists at the destination URI, it will not be overwritten.

request.Headers.Add("Overwrite", "F");

// Send the MOVE method request.

WebResponse Response = request.GetResponse();

// Close the HttpWebResponse object.

Response.Close();

 


  
public static bool IsSPOSite(string clientContextUrl) 
{

// SPO site

if (clientContextUrl.ToLower().Contains(".com")) 
{

return true; }

else {

return false; } 
}
 
 

No comments: