If you have a webpart or a custom form where you use the SharePoint peoplepicker you have to fetch the values of the control to place them into SharePoint.
This is how you do this.

1

 

2

When I press the save button on my form this code I need to use:

string[] userarray = ppTechnicalManager.CommaSeparatedAccounts.ToString().Split(‘,’);
SPFieldUserValueCollection usercollection = new SPFieldUserValueCollection();
for (int i = 0; i < userarray.Length; i++)
usercollection.Add(ConvertLoginAccount(userarray[i]));
item [“Technical Manager”]= usercollection;      
public SPFieldUserValue ConvertLoginAccount(string userid)
{
SPFieldUserValue uservalue;
SPSite site = new SPSite(SPContext.Current.Site.Url);
SPWeb web = site.OpenWeb();
SPUser requireduser = web.EnsureUser(userid);
uservalue = new SPFieldUserValue(web, requireduser.ID, requireduser.LoginName);
return uservalue;
}

And now the values will be saved into your list.

If you wwant to retrieve those values from your listitem again and display this into you control:

SPFieldUser userField = (SPFieldUser)list.Fields.GetField(Fields.TechnicalManager);
SPFieldUserValue fieldValue = (SPFieldUserValue)userField.GetFieldValue(item[Fields.TechnicalManager].CheckForNull());
ArrayList entityArrayList = new ArrayList();
PickerEntity entity = new PickerEntity();
if (fieldValue.User != null)
{
SPUser user = fieldValue.User;
entity.Key = user.LoginName.ToString();
}
else
{
SPGroup group = SPContext.Current.Web.Groups[fieldValue.LookupValue];
entity.Key = group.LoginName.ToString();
}
entityArrayList.Add(entity);
ppTechnicalManager.UpdateEntities(entityArrayList);

This checks for groups and users.

Hope this helps

I have putted this in a feature receiver event.
The code is actually very straight forword.

 

[Guid(“9ea3c6f6-f6f2-45e2-9cee-2e95e3019a34”)]
public class MUACChangeEventReceiver : SPFeatureReceiver
{

SPList historyList = null;
SPList taskList = null;

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSite site = properties.Feature.Parent as SPSite;
SPWeb web = site.RootWeb;
SPList list = web.Lists[“Project site creation”];

TryGetHistoryList(web);
TryGetTaskList(web);

//Get workflow by name
//SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates.GetTemplateByName(Resources.Workflow.WorkflowDisplayName, CultureInfo.CurrentCulture);

Guid workflowTemplateGuid = Resources.Workflow.WorkflowId;
SPWorkflowTemplate workflowTemplate = web.WorkflowTemplates[workflowTemplateGuid];

web.AllowUnsafeUpdates = true;

try
{
// Create workflow association
SPWorkflowAssociation workflowAssociation = SPWorkflowAssociation.CreateListAssociation(workflowTemplate, Resources.Workflow.WorkflowDisplayName, taskList, historyList);

// Set workflow parameters
workflowAssociation.AllowManual = false;
workflowAssociation.AutoStartCreate = true;
workflowAssociation.AutoStartChange = false;

// Add workflow association to my list
list.WorkflowAssociations.Add(workflowAssociation);

// Enable workflow
workflowAssociation.Enabled = true;
}
finally
{
web.AllowUnsafeUpdates = false;
}
}

private void TryGetHistoryList(SPWeb web)
{
try
{
historyList = web.Lists[“Workflow History”];
}
catch (ArgumentException exc)
{
// Create workflow history list
Guid listGuid = web.Lists.Add(“Workflow History”, “”, SPListTemplateType.WorkflowHistory);
historyList = web.Lists[listGuid];
historyList.Hidden = true;
historyList.Update();
}
}
private void TryGetTaskList(SPWeb web)
{
try
{
taskList = web.Lists[“Workflow Tasks”];
}
catch (ArgumentException exc)
{
// Create workflow tasks list
Guid listGuid = web.Lists.Add(“Workflow Tasks”, “”, SPListTemplateType.Tasks);
taskList = web.Lists[listGuid];
taskList.Hidden = true;
taskList.Update();
}
}
}

Most often you want your customsolution to be deployed to only a specific webapplication but SharePoint won’t allow it.

It’s actually very easy to fix this by tricking SharePoint.

2014-03-03_8-08-06.2png

In your package manifest you add a safe control:

<Solution xmlns=”http://schemas.microsoft.com/sharepoint/”&gt;
<Assemblies>
<Assembly Location=”dll_name_of_your_solution.dll” DeploymentTarget=”GlobalAssemblyCache”>
<SafeControls>
             <SafeControl Assembly=”dll_name_of_your_solution,Version=1.0.0.0,Culture=neutral, PublicKeyToken=****************” Namespace=”namespace_of_your_solution” TypeName=”*” />
</SafeControls>
</Assembly>
</Assemblies></Solution>

2014-03-03_8-07-02