function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShaikAShaikA 

How to insert FeedItem when code is executing in a Force.com Site?

Hi All,

Please let me know, how to insert feeditem for site user

Vf page publish in sites:
<apex:page controller="fileUploadController">
<apex:messages />
<apex:form id="frm">
    <apex:pageblock >
        <apex:pageblocksection columns="1">
             <apex:inputFile value="{!file}" filename="{!conVers.title}"/>
             <apex:commandbutton action="{!upload}" value="Upload" />
        </apex:pageblocksection>
    </apex:pageblock>   
</apex:form>
</apex:page>
Class:
public with sharing class fileUploadController{
public blob file { get; set; }
public ID accid{get;set;}
public ContentVersion conVers{get;set;}

 public fileUploadController(){
     accid=ApexPages.currentPage().getParameters().get('id');
     conVers= new ContentVersion();
 }
 public PageReference upload(){
  
        conVers.versionData = file;
        conVers.pathOnClient ='/'+conVers.Title;
        try
        {
        insert conVers;
        FeedItem elm = new FeedItem(Body = 'Post with related document body', ParentId = accid, RelatedRecordId =conVers.Id, Type = 'ContentPost');
        insert elm;
        }
        catch (DMLException e)
        {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading File'));
        return null;
        }
        finally
        {
        conVers= new ContentVersion();
        }
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
        return null;
    }
}

getting error like:
DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Entity is read-only: FeedItem: []

Thanks in Advance
Shaik 
NagendraNagendra (Salesforce Developers) 
Hi,

Please check with below link with similar issue. Hope this helps.

Regards,
Nagendra.