Home > Web Services > How to create item in SharePoint2010 document library using SharePoint Web service

How to create item in SharePoint2010 document library using SharePoint Web service

Today, I’d like to show you how to create item in SharePoint2010 document library using SharePoint Web service. Originally, I thought I could use the WebSvcLists(list.asmx) that provides methods for working with lists and list data. However, after a bit Googling , I realize that I need to use the WebSvcCopy (copy.asmx).Here are the code used

private const string siteUrl = "http://ybbest";

private static void Main(string[] args)

{

using (CopyWSProxyWrapper copyWSProxyWrapper = new CopyWSProxyWrapper(siteUrl))

{

copyWSProxyWrapper.UploadFile("TestDoc2.pdf",

new[] {string.Format("{0}/Shared Documents/TestDoc2.pdf", siteUrl)},

Resource.TestDoc, GetFieldInfos().ToArray());

}

}

private static List GetFieldInfos()

{

var fieldInfos = new List();

//The InternalName , DisplayName and FieldType are all required to make it work

fieldInfos.Add(new FieldInformation

{

InternalName = "Title",

Value = "TestDoc2.pdf",

DisplayName = "Title",

Type = FieldType.Text

});

return fieldInfos;

}

Note: When setting the metadata of the file(FieldInformation) , you need The InternalName , DisplayName and FieldType are all required to make it work

Here is the code for the proxy wrapper.


public class CopyWSProxyWrapper : IDisposable

{

private readonly string siteUrl;

public CopyWSProxyWrapper(string siteUrl)

{

this.siteUrl = siteUrl;

}

private readonly CopySoapClient proxy = new CopySoapClient();

public void UploadFile(string testdoc2Pdf, string[] destinationUrls, byte[] testDoc,

FieldInformation[] fieldInformations)

{

using (CopySoapClient proxy = new CopySoapClient())

{

proxy.Endpoint.Address = new EndpointAddress(String.Format("{0}/_vti_bin/copy.asmx", siteUrl));

proxy.ClientCredentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;

proxy.ClientCredentials.Windows.AllowedImpersonationLevel =

TokenImpersonationLevel.Impersonation;

CopyResult[] copyResults = null;

try

{

proxy.CopyIntoItems(testdoc2Pdf,

destinationUrls,

fieldInformations, testDoc, out copyResults);

}

catch (Exception e)

{

System.Console.WriteLine(e);

}

if (copyResults != null) System.Console.WriteLine(copyResults[0].ErrorMessage);

System.Console.ReadLine();

}

}

public void Dispose()

{

proxy.Close();

}

}

You can download the source code here .

******Update**********

It seems to be a bug that , you can not set the contentType when create a document item using Copy.asmx. In sp2007 the field type was Choice, however, in sp2010 it is actually Computed. I have tried using the Computed field type with no luck. I have also tried sending the ContentTypeId and this does not work.

******Update 20/04/2012**********

I was wrong in my previous update about unable to set the contenttype.As jasonkuter  points out in the comment,You can set the contentType by using the following code

fieldInfos.Add(new FieldInformation { InternalName = "ContentTypeId", Value = "0x010100866B1423D33DDA4CA1A4639B54DD464200DED91C30FFF44647AFA0F99FA6F0E2D2", DisplayName = "Content Type ID", Type = FieldType.Text });

You can find the contenttype id by using the SharePoint manager.Note: the contenttype id is the id in the document library not the content type id you defined in the site collection level.You can download the updated sourcecode here.

References:

SharePoint 2010 Web Services

SharePoint2007 Web Services

SharePoint MSDN Forum

About these ads
  1. March 23, 2012 at 7:02 am | #1

    This should work to update the contenttype in 2010:

    FieldInformation Type=”Text” DisplayName=”Content Type ID” InternalName=”ContentTypeId”

    • ybbest
      April 21, 2012 at 1:47 pm | #2

      Thanks , it work for content type this way.

  2. Larry Wilkinson
    May 2, 2012 at 6:12 am | #3

    Thanks, this worked for SP2010. Now I just need to find out how to get the ID programatically; when I do, I’ll post that also

  3. Larry Wilkinson
    May 2, 2012 at 6:31 am | #4

    calling list service with request GetListContentTypes and pointing to desired library (listName parameter)returns all Content Types – content type id is one of the returned fields

  4. dan
    August 25, 2012 at 6:14 pm | #5

    Cool article. I’m going to use this as part of a project I’m helping work on for a work mate of mine ;)

  5. Nicolas
    September 21, 2012 at 10:35 am | #6

    Doesn’t work for me. Content type is always default…

    • ybbest
      September 21, 2012 at 10:39 am | #7

      Note: the contenttype id is the id in the document library not the content type id you defined in the site collection level.

  6. Nicolas
    September 21, 2012 at 10:48 am | #8

    Yes I know. I tried the two Ids (Content Type ID and List Content Type Id). Does not work for me…

  7. Nicolas
    September 21, 2012 at 10:55 am | #9

    Does this need a particular version of SharePoint? (SP1, hotfix etc..)

  8. Nicolas
    September 21, 2012 at 6:58 pm | #11

    Hello,

    Thanks for your help and very quick answer.
    Just tried your code and same problem. Always Fruit document is applied (default content type).
    So I suspect a version difference. I use SharePoint Server 2010 RTM 14.0.4763.1000.

  9. mahesh
    November 4, 2013 at 10:57 pm | #12

    hi i have tried the above code i am getting the error.
    i am trying to store document in document library of office365online,
    i am getting exception at this line
    proxy.CopyIntoItems(testdoc2Pdf,destinationUrls,fieldInformations, testDoc, out copyResults);
    please can anybody tell what is this error.
    Thanking you in Advance
    Mahesh

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 155 other followers

%d bloggers like this: