Getting the sequential location id for first unit of course

I have been using below api which returns mfe link for lms:

def get_course_home_url(course_org, course_key):
    mfe_domain = SiteConfiguration.get_value_for_org(
        course_org,
        "LEARNING_MICROFRONTEND_URL",
        settings.LEARNING_MICROFRONTEND_URL
    )
    mfe_link = f'{mfe_domain}/course/{course_key}/home'
    log.info("mfe_link {}".format(mfe_link))
    return mfe_link

which returns one mfe link like this:

https://learning-xyz.lms.domain.com/learning/course/course_id/home

But instead i want url like this which contains the location id of first unit of the course:

https://lms-xyz.lms.domain.com/courses/course_id2024_2/jump_to/block-v1:part_of_course_id+type@sequential+block@77f114b31ea641bf96a77017e52f54a7

Here the problem is, i dont know how to get the sequential block id of first unit of the course using the api code i mentioned.

is there any workaround so that i can achieve this thing?

hi @Pratik_Ahir ,
import this

from xmodule.modulestore.django import modulestore
from openedx.features.course_experience.url_helpers import make_learning_mfe_courseware_url

i tried to do this like as , be sure course should have section ,subsection and atleast one unit or you can at add your custom conditions to handle this

def get_course_home_url(course_org, course_key):
    mfe_domain = SiteConfiguration.get_value_for_org(
        course_org,
        "LEARNING_MICROFRONTEND_URL",
        settings.LEARNING_MICROFRONTEND_URL
    )
    course_id = modulestore().get_course(course_key).id
    first_sequeceid = modulestore().get_course(course_key).get_children()[0].get_children()[0].get_metadata()['item_id']
    first_unit = modulestore().get_course(course_key).get_children()[0].get_children()[0].get_children()[0].scope_ids.usage_id
    mfe_link = make_learning_mfe_courseware_url(course_key=course_id, sequence_key=first_sequeceid , unit_key = first_unit)
    # mfe_link = f'{mfe_domain}/course/{course_key}/home'
    log.info("mfe_link {}".format(mfe_link))
    return mfe_link

hi @vivek Thanks for the reply.

first_sequeceid = modulestore().get_course(course_key).get_children()[0].get_children()[0].get_metadata()['item_id']

This line gives below error:

Traceback (most recent call last):
File “”, line 1, in
File “/openedx/edx-platform/xmodule/seq_module.py”, line 395, in get_metadata
if prereq_met and view == STUDENT_VIEW and not self._can_user_view_content(course):
File “/openedx/edx-platform/xmodule/seq_module.py”, line 562, in _can_user_view_content
self.runtime.service(self, ‘user’).get_current_user().opt_attrs.get(ATTR_KEY_USER_IS_STAFF) or
File “/openedx/edx-platform/xmodule/x_module.py”, line 1879, in service
service = self._descriptor_system.service(block, service_name)
File “/openedx/edx-platform/xmodule/x_module.py”, line 1241, in service
service = super().service(block=block, service_name=service_name)
File “/openedx/venv/lib/python3.8/site-packages/xblock/runtime.py”, line 1125, in service
raise NoSuchServiceError(f"Service {service_name!r} is not available.")
xblock.exceptions.NoSuchServiceError: Service ‘user’ is not available.

It works fine till modulestore().get_course(course_key).get_children()[0].get_children()[0], but when i try to get metadata then it arises.

try this
first_sequeceid = modulestore().get_course(user_course_outline.course_key).get_children()[0].get_children()[0].scope_ids.usage_id
instead of this

this solution will not work either as user_course_outline is dependent on get_user_course_outline which further raise a dependency issue.

Actually we have found the solution just by constructing url like this and it worked as expected:

https://lms-xyz.lms.domain.com/courses/course_id/jump_to/block-v1:part_of_course_id+type@course+block@course

good to know, thankyou