Programmatically closing an Opportunity object is pretty easy in CRM 4.0, although it took me some time to figure out how to do it correctly.
Key key = (Key)entity.Properties["opportunityid"];
opportunityclose close = new opportunityclose();
close.opportunityid = new Lookup();
close.opportunityid.Value = key.Value;
WinOpportunityRequest request = new WinOpportunityRequest();
request.OpportunityClose = close;
request.Status = -1;
catch (Exception e)
string onbreak = e.Message;
This is pretty straightforward. Create an opportunityclose object, create a WinOpportunityRequest object, set their properties and execute the request.
The reason we set the request.Status to –1 for the system default for either losing or winning the opportunity.