Issue in progrmatically setting target audience in quick launch in sharepoint 2019 classic view

Mayank Bhargava 21 Reputation points
2022-10-19T03:39:32.097+00:00

Currently, I am able to add a single SharePoint user or group to "Audience" property quick launch using below code but my requirement is to add multiple SharePoint Group or user in "Audience" property is this possible programmatically?
Please advise

for (int i = 0; i < quickLinks.Count; i++)
{
SPNavigationNode navNode = new SPNavigationNode(Convert.ToString(quickLinks[i].LinkTitle), Convert.ToString(quickLinks[i].LinkUrl), true);
nodes.AddAsFirst(navNode);
if (quickLinks[i].LinkTitle == "Insert Task")
{
navNode.Properties.Add("Audience", ";;;;" + Constant.Project_Managers);
//navNode.Properties.Add("Audience", ";;;;" + Constant.Project_Managers + ";" + Constant.Secreteriate + ";" + Constant.Team_Leader);
}
navNode.Update();
}

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
2,758 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,590 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 32,476 Reputation points Microsoft Vendor
    2022-10-19T08:53:56.82+00:00

    Hi @Mayank Bhargava
    As far as I know, navNode.Properties.Add("Audience", user); can only add one user at same time. There is no such function to add multiple SharePoint Groups or users at same time directly. As a workaround, we can store users in a collection and add them one by one like following code

    foreach (var user in users){  
        navNode.Properties.Add("Audience", user);  
    }         
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



  2. RaytheonXie_MSFT 32,476 Reputation points Microsoft Vendor
    2022-10-27T02:12:52.88+00:00

    Hi @Mayank Bhargava
    Thanks for your sharing ,if you have any issue about SharePoint in the future, you are welcome to raise a ticket in this forum.

    By the way, since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others." and according to the scenario introduced here: Answering your own questions on Microsoft Q&A, I would make a brief summary of this thread:

    [Issue in progrmatically setting target audience in quick launch in sharepoint 2019 classic view]

    Issue Symptom:
    Unable to set multiple SPUSer or SPGroup in Audience programmatically.

    Current status:
    We can only set single SPUSer or SPGroup to a Audience property. navNode.Update(); will overwrites the existing Audience property

    You could click the "Accept Answer" button for this summary to close this thread, and this can make it easier for other community member's to see the useful information when reading this thread. Thanks for your understanding!

    0 comments No comments