cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ryan13
Community Member

Error adding assignment through API

Jump to solution

Hello I am new to canvas and I am creating an external tool that creates modules and module items to be sent to canvas.

I am trying to add assignments to my module using php.

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, '<canvas>/api/v1/courses/<courseid>/modules/<moduleid>/items');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "module_item[content_id]=1&module_item[title]=module item&module_item[type]=Assignment&module_item[position]=2&module_item[indent]=1&module_item[external_url]=<myexternalurl>");
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = 'Authorization: Bearer '. $token;
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
echo $result;
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

Other module types like External Tool etc. are working except assignments and it always throw this message

{"message":"Could not find content"}

Need help with this

1 Solution

Accepted Solutions
James
Community Champion

I suspect two things.

One may be that you're creating a type of Assignment but then giving it an ExternalURL. How do you have an external URL with an assignment?

The other thing is to make sure that you're using the correct ID. That seems more likely since you got the error "could not find content." The other part of this is that the content_id and content_type have to go together in the same course. You can only use content_id=1 with type=Assignment if there is an Assignment in that course with an ID=1. Click on the assignment you want and note the assignment_id at the end of the URL. Then use that ID for the content_id.

View solution in original post

4 Replies
James
Community Champion

I suspect two things.

One may be that you're creating a type of Assignment but then giving it an ExternalURL. How do you have an external URL with an assignment?

The other thing is to make sure that you're using the correct ID. That seems more likely since you got the error "could not find content." The other part of this is that the content_id and content_type have to go together in the same course. You can only use content_id=1 with type=Assignment if there is an Assignment in that course with an ID=1. Click on the assignment you want and note the assignment_id at the end of the URL. Then use that ID for the content_id.

View solution in original post

ryan13
Community Member

Thank you. I changed the content_id to an existing Assignment and it worked. So I have to create assignments too for each module Items with type assignments in my tool? I added External URL because I thought that it will set the Submission type of the assignment to external tool and pointing to that link, I thought I had it working before but I'm not sure.

James
Community Champion

 @ryan13 ,

The endpoint you're using is just for adding items to a module. It's the create a module item endpoint of the modules API.

The type of content has nothing to do with the submission type. That's specified in the assignment itself. All this endpoint does is add it to a module.

You can have File, Page, Discussion, Assignment, Quiz, SubHeader, ExternalUrl, or ExternalTool. The File, Page, Discussion, Assignment, and Quiz must be content that already exists within Canvas. The SubHeader is simply text. The ExternalUrl points is just a link and usually resides outside Canvas (if it were inside the Canvas course, then you would typically use one of the other types). The ExternalTool needs to be configured in the Apps for either the account or the course.

Some things are not used with all module items, but Canvas isn't clear in the way they define that. For example, the page_url is only for the Page type. Canvas says it's "Required for 'Page' type" but that makes it sound like it's optional for other things. In this case, it's only valid for Page type and you should not specify it for other types.

Similarly, external_url would only be used when your type is ExternalUrl or ExternalTool.

Most of the types need a content_id that links to an existing item. That's what Canvas means when the docs say "The id of the content to link to the module item. Required, except for 'ExternalUrl', 'Page', and 'SubHeader' types."

ryan13
Community Member

What I did is to check if the module type if assignment and create the assignment first to generate the content_id needed for that module item. Thank you.