Developing Mass Assignment feature: error create assignment from code

Topics: Developer Forum
Apr 8, 2012 at 12:44 PM
Edited Apr 8, 2012 at 12:49 PM

I'm developing "Mass Assignment" feature for one of my customers. This creates many assignments to many users (MxN) at a time. I took AssignmentProperties.aspx.cs as a base of my development - it has right sequence of calls to create an assignment, so I did mostly copy-paste/

Problem is: I try to create new assignment in my code, but this fails with error:

System.InvalidOperationException: Invalid value for property 'InstructorId': Cannot be null.
at Microsoft.LearningComponents.Storage.LearningStoreValueType.CastValue(Object value, IFormatProvider formatprovider, GetCastException getCastException)
at Microsoft.LearningComponents.Storage.LearningStoreJob.AddItem(String itemTypeName, IDictionary`2 propertyValues, Boolean requestItemId)
at Microsoft.SharePointLearningKit.SlkStore.CreateAssignment(AssignmentProperties properties)
at Microsoft.SharePointLearningKit.ApplicationPages.MassAssignment.Assign_Process()

I've drilled down the code as deep as possible - in function LearningStoreJob.AddItem() I found this:

    // create one InstructorAssignmentItem for each instructor of the assignment
   foreach (SlkUser instructor in properties.Instructors)
   {
      Dictionary<string, object> instructorProperties = new Dictionary<string, object>();
   instructorProperties[Schema.InstructorAssignmentItem.AssignmentId] = tempAssignmentId;
      instructorProperties[Schema.InstructorAssignmentItem.InstructorId] = instructor.UserId;
      job.AddItem(Schema.InstructorAssignmentItem.ItemTypeName, instructorProperties);
   }

- the place where InstructorId is being set. I checked the variable - it's NOT NULL. But code still fails.

I use following sequence (code is shortened to be readable):

    SharePointFileLocation location = new SharePointFileLocation(locationString);
   assig = AssignmentProperties.CreateNewAssignmentObject(slkStore, spWeb, SlkRole.Instructor);
   assig.SetLocation(location, 0, null);

    assig.Instructors.Add(instructor); //I'm sure 'instructor' variable is OK.
   assig.Learners.Add(learner);

AssignmentItemIdentifier id = this.SlkStore.CreateAssignment(assig);

 

Any ideas?

Apr 8, 2012 at 4:54 PM
Edited Apr 8, 2012 at 4:56 PM

Well, I'm answering to myself.

When you use SlkMemberships to store list of instructors, you probably use SlkMemberships.AddUser(SPUser, ****)

This method has bug, for sure - when you add user as instructor, the instance of SPUser adds well, but InstructorId field for user is not set inside AddUser, but should.

So, I've created same method AddUser with first parameter SlkUser instead of SPUser - and now everything is fine, as it should be. Complete method is below:

 

public class SlkMemberships
{
        public void AddUser(SlkUser user, bool isInstructor, bool isLearner, SlkGroup learnerGroup, List<string> learnerKeys)
        {
            SlkUser slkUser2;
            string userKey = user.Key;

            if (users.TryGetValue(userKey, out slkUser2))
                user = slkUser2;
            else
                users[userKey] = user;

            if (isInstructor)
                instructors[userKey] = user;

            if (isLearner)
            {
                learners[userKey] = user;

                if (learnerGroup != null)
                {
                    learnerGroup.UserKeys.Add(userKey);
                }

                if (learnerKeys != null)
                {
                    learnerKeys.Add(userKey);
                }
            }
        }
}


Jan 3, 2014 at 6:43 PM
I'm in the same situation. Could you provide the code you used? Or at least tell me how you added your solution?

Thanks