Showing posts with label Document Library. Show all posts
Showing posts with label Document Library. Show all posts

Exception: Only String, int, and DateTime data types can be used as the value in Properties while adding list item in document library.

This is the exception I got when I try to add a list item through c# in a SharePoint document library.

Details:

In my previous post, I explained the logic for adding list item to a document library through c# code. This works well if the list/library columns are of type int, string and date time. If I add a column of type Currency then the code will break and it won't add the list item to the library at all. Because the function list.RootFolder.Files.Add() will not accept the properties other than the 3 types mentioned, through HashTable. This is the problem with the function. To avoid that below is the code i used and this problem went away.

Solution:

SPFile destfile = list.RootFolder.Files.Add(
fileName.Substring(fileName.LastIndexOf("\\") + 1), fs, true);
if (destfile.Item == null)
lblMsg.Text = "Error in adding file";
else
{
SPListItem listItem = destfile.Item;

//Logic to set list item columns like
//listItem["Title"] = "Document Title";
//listItem["CurrencyField"] = 234.908;
//listItem["NumberField"] = 12345.678;

listItem["ContentType"] = "Document"; //Default Content type for Document libraries
listItem.Update();
destfile.Update();
}

If you observe the code difference between my previous post and this post is, I am not passing the HashTable object to list.RootFolder.Files.Add() method. Because there the problem was. [Exception was coming from it.]

After I am adding the document then trying to set the column names of the list item by reading the same list item object which is from the line

SPListItem listItem = destfile.Item;

The below line to this line of code is for setting the content type of the document library. Take care of this one that, a document library can inherit from more than one content types. So you should mention the content type for better results.

Finally, updating the list item and updating the file to reflect the changes to the document library list items. Please feel free to post your ideas, comments and question here.

Read More...

Check out causing the item update event receivers call twice in SharePoint

Event receivers will help us to write events to add custom logic to perform operations while adding/updating/deleting items in a list/libraries in SharePoint. We can register the receivers to all lists/libraries or to a particular list or a library. Here in this post I am explaining the special case of the item update event receivers.

This is regarding the calling of these events 2 times by SharePoint framework when check out action done on a document library item. When you enable the option Require Check Out in a document library and when you choose option to checkout a document in a document library then it will call the item update events twice [If you write any update event receivers on the list then only]. So take care to avoid the unwanted call, otherwise it will do some unwanted actions. Because of it there may be chances of performance issue. Below are the details of solving the issue.

If the vti_sourcecontrolcheckedoutby property exits in the "BeforeProperties" property but not in the "AfterProperties" property, the event was caused by checking in a document. So by using this logic we will put the logic in specific block.

if (properties.AfterProperties["vti_sourcecontrolcheckedoutby"] == null
&& properties.BeforeProperties["vti_sourcecontrolcheckedoutby"] != null)
{
    //This is when the update event is triggered by check-in.
}
else
{
    //This is triggered by events other than check-in action.
}

For more details:

http://support.microsoft.com/kb/939307

Read More...

Adding a list item to Document library through c# in SharePoint 2007

As we all know the required filed for document library is not Title. We should be upload a file for the document library list item. This is the required field by default. You can add columns and make them required as well. So, when through program we want to add a list item to the document library then we need to collect the uploaded file and the meta data i.e. the list of columns data. So on the custom form of the document library we need to add ASP.NET controls for file upload and for all the other columns. Here I thought of giving C# code we need to use to create a list item in SharePoint document library. I think this is the only efficient way of adding a list item to document library. If not please post your ideas.

string fileName = fileUpload.PostedFile.FileName;
using (SPSite site = new SPSite("http://sharepointserver"))
{
using (SPWeb web = site.OpenWeb("/"))
{
try
{
web.AllowUnsafeUpdates = true;
using (FileStream fs = File.Open(fileName, FileMode.Open))
{
SPList list = web.Lists["Documents"];
Hashtable metaData = new Hashtable();
for (int i = 0; i < keys.Count; i++)
{
metaData.Add(keys[i], values[i]);
}
SPFile destfile = list.RootFolder.Files.Add(fileName.Substring(fileName.LastIndexOf("\\") + 1),
fs, metaData, true);
if (destfile == null)
lit.Text = "Error in adding file";
}
}
catch
{ }
finally
{
web.AllowUnsafeUpdates = false;
}
}
}

NOTE: fileUpload is the control I am using in my code to get the file from the custom form. And the key and value fields are the column label and it’s value respectively. If you know the list column names then remove for loop and hard code the label, value while adding them to the metadata. Example like below code,

metaData.Add("Title", "DemoDocument"); metaData.Add("Version", "1.0"); metaData.Add("Author", "Praveen"); metaData.Add("ContentType", "Document");

NOTE: There is a problem in adding the list item through the code if your list columns are not inheriting from the data types string, int and datetime. You will get an exception. So follow this post to fix it.

Please post your ideas on it.

Read More...
Related Posts with Thumbnails
GiF Pictures, Images and Photos