2

I'm trying to create a C# script that will allow me to import blog posts from a different system (exported as CSV) into a SharePoint blog. I have found a script for comments to a blog post, but I'm having trouble finding the write fields/syntax for posts themselves.

public static void AddComment(string url)
{
    using (SPSite site = new SPSite(url))
    {
        site.AllowUnsafeUpdates = true;
        using (SPWeb web = site.OpenWeb())
        {
            web.AllowUnsafeUpdates = true;
            SPList commentList = web.Lists["Comments"];
            SPListItem newItem = commentList.AddItem();
            newItem["Body"] = "body";
            newItem["Title"] = "title";
            newItem["PostTitle"] = "2;#post1";
            newItem.Update();
        }
    }
}

From the above code for adding comments, I see that it's a simple SPListItem. Plus, it appears that the link between comments and posts is the PostTitle, but in this example is 2;#post1 pointing to post with ID=2, or does that post have a title of "2" (i.e. would `PostTitle = "My Title;#post 4" work, where my post has the title "My Title" and this particular comment is the 4th one?

Any links/code/advice would be appreciated.

Kevin

2 Answers 2

4

2;#post1 is a SPFieldLookupValue where the LookupValue is "post 4" and the LookupId is 2.

The LookupId of the item being looked up is the list item ID in the list it's being stored in.

To set the value of a lookup field you have a few options:

newItem["PostTitle"] = 2; // Just pass the lookup ID
newItem["PostTitle"] = new SPFieldLookupValue("2;#post1"); // string of lookup field value
newItem["PostTitle"] = new SPFieldLookupValue(2, "post1"); // constructor with ID, value
1
  • Thanks - this helps with the comments. Do you have any info on how to add blog posts themselves? This was the focus of the question, though I realize I did talk about comments a lot.
    – QMKevin
    Commented Jan 14, 2013 at 16:48
3

I got it working, with help from rjcup's answer (which help me link them together). here's my working (uncleaned) code:

    protected void btnStart_Click(object sender, EventArgs e)
    {
        string qmSPSiteURL = ConfigurationManager.AppSettings["qmSPSiteURL"];
        using (SPSite site = new SPSite(qmSPSiteURL + "/communityspaces/authoring-zone/blog"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                DateTime created = DateTime.Now;
                int author = 22;

                SPList postList = web.Lists["Posts"];
                SPListItem newPost = postList.AddItem();
                newPost["Title"] = "New blog added programmatically";
                newPost["Body"] = "This post was added using c-sharp";
                newPost["Created"] = created;
                newPost["Author"] = author;
                newPost[SPBuiltInFieldId.Editor] = author;
                newPost.ModerationInformation.Status = SPModerationStatusType.Approved;
                newPost[SPBuiltInFieldId.PublishedDate] = created;
                newPost.Update();

                int postID = newPost.ID;

                addComment(postID);
            }
        }
    }

    protected void addComment(int postID)
    {
        string qmSPSiteURL = ConfigurationManager.AppSettings["qmSPSiteURL"];
        using (SPSite site = new SPSite(qmSPSiteURL + "/communityspaces/authoring-zone/blog"))
        {
            using (SPWeb web = site.OpenWeb())
            {
                DateTime created = DateTime.Now.AddMonths(-8);
                int author = 72;

                SPList commentList = web.Lists["Comments"];
                SPListItem newComment = commentList.AddItem();
                newComment["Body"] = "This comment was added using c-sharp";
                newComment["Title"] = "New comment added programmatically";
                newComment["PostTitle"] = postID.ToString();
                newComment["Created"] = created.ToString();
                newComment["Author"] = author;
                newComment[SPBuiltInFieldId.Editor] = author;
                newComment.Update();
            }
        }
    }

Hope it helps someone :)

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.