Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8548257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:27:11+00:00 2026-06-11T13:27:11+00:00

I want to programmatically lock all content controls so that the user cannot delete

  • 0

I want to programmatically lock all content controls so that the user cannot delete them.

I am using the following code, but my problem is that in a few cases I am getting null when calling elem.SdtProperties.ChildElements.First<WP.Lock>().

Can someone help me complete the below mentioned code?

static void MakeContentControlsNonDeletable()
        {
            using (P.WordprocessingDocument wordDoc =
              P.WordprocessingDocument.Open(@"c:\XYZ.docx", true))
            {
                IEnumerable<WP.SdtElement> elements =
                  wordDoc.MainDocumentPart.Document.Descendants<WP.SdtElement>();

                foreach (WP.SdtElement elem in elements)
                {
                    if (elem.SdtProperties != null)
                    {
                        WP.Lock l = elem.SdtProperties.ChildElements.First<WP.Lock>();

                        if (l == null)
                        {
                            //Please help here
                            //Please help here
                            //Please help here
                            //Please help here
                        }

                        if (l.Val != WP.LockingValues.SdtContentLocked && l.Val != WP.LockingValues.SdtLocked)
                        {
                            Console.WriteLine("Unlock content element...");
                            l.Val = WP.LockingValues.SdtLocked;
                        }
                    }
                }
            }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:27:13+00:00Added an answer on June 11, 2026 at 1:27 pm

    It seems your code is fine. I’ve had the same problems before in another scenario where there is a certain object and it returns null. I don’t know what’s wrong with openxml sdk at that point but I can tell you how I did solve mine.

    The problem basically is that at certain points deep in the structure you know there is a element that should be interpreted as a Lock object but the sdk can see it only as OpenXmlElement (not its subclass Lock object), so thats why when you do First<WP.Lock>() you expect a Lock object and you know its there but you just get null. What I do is treat everything as OpenXmlElement and forget about the strong typing.

    static void MakeContentControlsNonDeletable()
        {
            using (P.WordprocessingDocument wordDoc =
              P.WordprocessingDocument.Open(@"c:\XYZ.docx", true))
            {
                IEnumerable<OpenXmlElement> elements =
                  wordDoc.MainDocumentPart.Document.Descendants<>(child => child.LocalName == "sdt");
    
                foreach (OpenXmlElement elem in elements)
                {
                    if (elem.ChildElements.Any(child => child.LocalName == "sdtPr"))
                    {
                        OpenXmlElement sdtPr = elem.ChildElements.FirstOrDefault(child => child.LocalName == "sdtPr");
                        OpenXmlElement l = sdtPr.ChildElements.FirstOrDefault(child => child.LocalName == "lock");
    
                        if (l == null) //At this point if you have your lock object this isn't null
                        {
                            //Please help here
                            //Please help here
                            //Please help here
                        }
    
                        OpenXmlAttribute valAttribute = l.GetAttribute("val", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
                        if (valAttribute != null) {
                             valAttribute = new OpenXmlAttribute();
                        }
    
                        if (valAttribute.Value != "sdtContentLocked" && valAttribute.Value != "sdtLocked")
                        {
                            Console.WriteLine("Unlock content element...");
                            valAttribute.Value = "sdtLocked";
                        }
                    }
                }
            } 
    

    I know this isn’t the way it’s supposed to be and I know that all objects should be strongly typed to its respective openxml sdk class but that’s something that happens a lot and that’s why I do it this way.

    Hope it helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to programmatically modify a bitmap using python but don't really need a
I want to programmatically download the contents of a web page, but that page
I want to programmatically edit file content using windows command line ( cmd.exe ).
I'm using jQuery 1.6.1 and I have checkbox that I want to programmatically check
I want to programmatically create a directory on the server using ASP.NET. I have
I want to write the code on how to unlock the Android Phone programmatically.
I want to automate a job involving font lock mode using Emacs. I want
I want to lock the orientation programmatically for my application screens to be displayed
I want to programmatically add Text Views controls to my home screen widget. In
I want to programmatically publish messages to my site user's facebook page on their

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.