BlogEngine.Core.API.BlogML.BlogImporter.AddPost C# (CSharp) Méthode

AddPost() public méthode

Add new blog post to system
public AddPost ( BlogMlExtendedPost extPost ) : string
extPost BlogMlExtendedPost
Résultat string
        public string AddPost(BlogMlExtendedPost extPost)
        {
            if (!Security.IsAdministrator)
            {
                throw new InvalidOperationException("BlogImporter.AddPost: Wrong credentials");
            }

            using (var p = new Post())
            {

                if (!string.IsNullOrEmpty(extPost.PostUrl))
                {
                    // looking for a Slug with patterns such as:
                    //    /some-slug.aspx
                    //    /some-slug.html
                    //    /some-slug
                    //
                    Match slugMatch = Regex.Match(extPost.PostUrl, @"/([^/\.]+)(?:$|\.[\w]{1,10}$)", RegexOptions.IgnoreCase);
                    if (slugMatch.Success)
                        p.Slug = slugMatch.Groups[1].Value.Trim();
                }

                if (extPost.Categories != null && extPost.Categories.Count > 0)
                    p.Categories.AddRange(extPost.Categories);

                if(extPost.Tags != null && extPost.Tags.Count > 0)
                    p.Tags.AddRange(extPost.Tags);

                // skip if post with this url already exists
                var s = PostUrl(p.Slug, p.DateCreated);
                var list = Post.Posts.FindAll(ps => ps.RelativeLink == s);
                if (list.Count > 0)
                {
                    return string.Empty;
                }

                if(extPost.Comments != null && extPost.Comments.Count > 0)
                {
                    foreach (var comment in extPost.Comments)
                    {
                        p.ImportComment(comment);
                    }
                }

                p.Import();
                return p.Id.ToString();
            }
        }

Usage Example

Exemple #1
0
        /// <summary>
        /// Loads the blog posts.
        /// </summary>
        private void LoadBlogPosts()
        {
            var bi = new BlogImporter();

            Utils.Log("BlogReader.LoadBlogPosts: Start importing posts");

            foreach (BlogMlExtendedPost extPost in blogsExtended)
            {
                try
                {
                    BlogMlExtendedPost post = extPost;

                    if (extPost.BlogPost.Categories.Count > 0)
                    {
                        for (var i = 0; i < extPost.BlogPost.Categories.Count; i++)
                        {
                            int i2  = i;
                            var cId = new Guid(post.BlogPost.Categories[i2].Ref);

                            foreach (var category in categoryLookup)
                            {
                                if (category.Id == cId)
                                {
                                    if (extPost.Categories == null)
                                    {
                                        extPost.Categories = new StateList <Category>();
                                    }

                                    extPost.Categories.Add(category);
                                }
                            }
                        }
                    }

                    if (!string.IsNullOrEmpty(bi.AddPost(extPost)))
                    {
                        PostCount++;
                    }
                    else
                    {
                        Utils.Log("Post '{0}' has been skipped" + extPost.BlogPost.Title);
                    }
                }
                catch (Exception ex)
                {
                    Utils.Log("BlogReader.LoadBlogPosts: " + ex.Message);
                }
            }
            bi.ForceReload();
            Utils.Log(string.Format("BlogReader.LoadBlogPosts: Completed importing {0} posts", PostCount));
        }
All Usage Examples Of BlogEngine.Core.API.BlogML.BlogImporter::AddPost