Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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
share|improve this answer
    
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 Jan 14 '13 at 16:48

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 :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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