1-888-365-2779
+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / SaveContent(IContent) doesn't publish the content

SaveContent(IContent) doesn't publish the content

4 posts, 0 answered
  1. Shane
    Shane avatar
    28 posts
    Registered:
    27 Jul 2008
    04 Dec 2008
    Link to this post
    Hi Guys,

    I need to create content dynamically. here is an example of my code:

    var content = Content.CreateContent("text/html");

                content.Content = "This is an <b>example</b> of content...";

                content.SetMetaData(Resource.BusinessUnitFieldName, "Business");
                content.SetMetaData(Resource.BusinessUnitImageUrlFieldName, "http://xxxx-A58268C56701/0/h.gif");
                content.SetMetaData(Resource.PublicationDateFieldName, DateTime.Now);
                content.SetMetaData(Resource.ExpirationDateFieldName, DateTime.Now.AddMonths(2));
                content.SetMetaData(Resource.GradeFieldName, "S3");
                content.SetMetaData(Resource.LocationFieldName, "Wellington");
                content.SetMetaData(Resource.ReferenceFieldName, "#1233214");
                content.SetMetaData(Resource.TitleFieldName, "Content Manager 2");

                Content.SaveContent(content);

    Please Note: The Content Property gives me access to a custom generic content item.

    /// <summary>
            /// Gets the instance of ContentManager class (which is used for working with data in 
            /// Generic Content based modules) and initializes it with proper Vacancies module provider
            /// </summary>
            private ContentManager Content
            {
                get
                {
                    if (contentManager == null) contentManager = new ContentManager(providerName);
                    return contentManager;
                }
            }

    When I save the content I want it to be published and ready for public viewing. But it is saved as Draft?

    Can you please help? Do I need to get workflow envolved via the api?

    Cheers,

    Shane
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    05 Dec 2008
    Link to this post
    Hello Shane,

    Thank you for using our services.

    As I see everything seems fine. It is possible the content to be saved in Drafts if you have workflow enabled or use  content.Status = ContentStatus.Draft somewhere. If so you need to to iterate through all workflow states, until we reach published state.

    Here is an example how to do this. I am working with news here.

    rotected void Page_Load(object sender, EventArgs e)  
        {  
            //We are going to create a new News Item  
            //First, we should create the manager with the provider we want  
            NewsManager newsManager = new NewsManager("News");  
            //Then, we have to create new content  
            IContent submittedNews = newsManager.Content.CreateContent("text/html");  
            DateTime mydate = new DateTime();  
            mydate = DateTime.Today;  
            submittedNews.SetMetaData("Title""MyTitle");  
            submittedNews.SetMetaData("Publication_Date", mydate );  
            submittedNews.SetMetaData("Author""MyName");  
      
            submittedNews.Content = "Here Goes My Content";  
      
            //save the news item as draft.  
            submittedNews.Status = ContentStatus.Draft;  
            newsManager.Content.SaveContent(submittedNews);  
           //iterate through all workflow states 
        
            StagedContent staged = newsManager.Content.GetCurrentState(submittedNews.ID);  
            //On each step we should clear the Nolics cache...  
            ExecuteActivity(staged.WorkflowInstanceId, "SendForApproval");  
            Nolics.ORMapper.Base.DataConnection.InitWebRequest();  
      
            ExecuteActivity(staged.WorkflowInstanceId, "Approve");  
            Nolics.ORMapper.Base.DataConnection.InitWebRequest();  
      
            ExecuteActivity(staged.WorkflowInstanceId, "Publish");   
      
        }  
      
        private EventActivity FindActivity(string name, IList<Activity> activities)  
        {  
            foreach (Activity act in activities)  
            {  
                if ((act is EventActivity) && (((EventActivity)act).CommandName == name))  
                    return (EventActivity)act;  
                EventActivity ea = FindActivity(name, act.Activities);  
                if (ea != null)  
                    return ea;  
            }  
            return null;  
        }  
      
        private void ExecuteActivity(Guid id, string activityName)  
        {  
            WorkflowInstance instance = WorkflowRuntime.Instance.GetWorkflow(id);  
            if (instance != null)  
            {  
                ContentWorkflow workflow = (ContentWorkflow)instance.Activity;  
                EventActivity activity = FindActivity(activityName, workflow.Activities);  
                if (activity != null)  
                {  
                    activity.Execute(WorkflowRuntime.Instance);  
                }  
            }  
        }   

    I hope this helps.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Shane
    Shane avatar
    28 posts
    Registered:
    27 Jul 2008
    07 Dec 2008
    Link to this post
    Thats for the reply.

    But I am still wondering why I can't just save a content item and have it published in the same call? 

    The code example you provided seems too complicated. Shouldn't there be (if any) a simple call like:

     StagedContent staged = Content.GetCurrentState(content.ID);
     staged.SetWorkFlow("Published");

    The code you supplied did work. But it seem too complicated and should be handled internally. The API should hide that process? So we can just call 'SetWorkFlow' etc.

    I do have another question? When I call the SaveContent method and pass in a ContentStatus enum set to Published it isn't automatically published? The content still has a status of draft?

    Any help would be great.

    Thanks,

    Shane

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    08 Dec 2008
    Link to this post
    Hi Shane,

    I cannot find a way to escape from iterating through the workflow. The content is always saved in drafts or it is locked by the user( let's say the admin). If someone of the community members find a easiest way than posted before, please share it with the other members

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
4 posts, 0 answered