0

I am trying to make a code that copies Posts and Comments to a site. It is working, except setting the lookup field to connect Comment with Post.


using Microsoft.SharePoint;
using System;
namespace SPTools
{
public static class CopyBlog
   {
    public static void CopyItems(string sourceurl, string targeturl)
    {
        SPWeb Sourceweb = new SPSite(sourceurl).OpenWeb();
        SPWeb Targetweb = new SPSite(targeturl).OpenWeb();

        foreach (SPListItem PostItem in Sourceweb.Lists["Posts"].Items)
        {
            // Copy Post
            SPListItem newPostItem = Targetweb.Lists["Posts"].Items.Add();
            newPostItem["Created"] = PostItem["Created"];
            newPostItem["Modified"] = PostItem["Modified"];
            newPostItem["Author"] = PostItem["Author"];
            newPostItem["Editor"] = PostItem["Editor"];
            for (int i = 0; i < PostItem.Fields.Count; i++)
            {
                // Copy all open field values
                if (!newPostItem.Fields[i].ReadOnlyField && newPostItem.Fields[i].InternalName != "Attachments")
                {
                    newPostItem[newPostItem.Fields[i].InternalName] = PostItem[newPostItem.Fields[i].InternalName];
                }
            }
            Console.Write("Updating Post: " + newPostItem.Title);
            newPostItem.Update();
            Console.WriteLine(" - Done");
            Console.WriteLine("Adding Comments: ");
            // Add Comments to the Post


            foreach (SPListItem CommentItem in Sourceweb.Lists["Comments"].Items)
            {
                Console.WriteLine("     Comments Post ID: " + CommentItem["PostID"]);
                if (CommentItem["PostID"].ToString() == PostItem.ID + ";#" + PostItem.ID)
                {
                    SPListItem newCommentItem = Targetweb.Lists["Comments"].Items.Add();
                    newCommentItem["Created"] = CommentItem["Created"];
                    newCommentItem["Modified"] = CommentItem["Modified"];
                    newCommentItem["Author"] = CommentItem["Author"];
                    newCommentItem["Editor"] = CommentItem["Editor"];

                    Console.WriteLine("******** New Value " + newPostItem.ID);
                    newCommentItem["PostID"] = new SPFieldLookupValue(newPostItem.ID, string.Empty);

                    for (int iComment = 0; iComment < CommentItem.Fields.Count; iComment++)
                    {
                        Console.WriteLine(iComment + "\t" + newCommentItem.Fields[iComment].TypeAsString);
                        if (!CommentItem.Fields[iComment].ReadOnlyField && CommentItem.Fields[iComment].InternalName != "Attachments")
                        {
                            newCommentItem[CommentItem.Fields[iComment].InternalName] = CommentItem[CommentItem.Fields[iComment].InternalName];
                        }
                    }
                    newCommentItem.Update();
                }
            }
        }
    }
}

Error: Invalid data has been used to update the list data...

If I remove the line with SPFieldLookupValue, it is working. Any suggestions to what I am missing?

1 Answer 1

0

Instead of PostID you should be updating PostTitle

For adding comment you can use below snippet

protected void addComment(SPWeb TargetWeb, SPListItem CommentItem, int postID)
{
    SPListItem newCommentItem = Targetweb.Lists["Comments"].Items.Add();
    newCommentItem["Created"] = CommentItem["Created"];
    newCommentItem["Modified"] = CommentItem["Modified"];
    newCommentItem["Author"] = CommentItem["Author"];
    newCommentItem["Editor"] = CommentItem["Editor"];
    newComment["PostTitle"] = postID.ToString();
    newComment.Update();
}

How to add a blog post programmatically?

2
  • Great! I spend 4 hours solving this issue. Why did I only search for lookupfield and not blog... Problem solved.
    – CADmageren
    Jan 13, 2015 at 20:59
  • @CADmageren nice to hear it helped! Jan 13, 2015 at 21:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.