Set GUID Programatically for New Audience
Question
I am Creating New Audience programmatically but I want to set its GUID by code (my own)
Is it possible to do this ? if yes then how ?
Solution
AudienceID is an internal field set by SharePoint. You can't set it, but you can retrieve it if it has been saved to the db.
What is your requirement that means you have to control the audience ID? Typically you would retrieve Audience by name. If you need to you can then access the AudienceID value.
Hope that helps.
OTHER TIPS
Please refer link below link Mr. Raju
You will find that it is not possible because SharePoint manages GUID internally. so you can not assign it. in some cases it is possible but in your case it is not possible.
I was able to do this. I created a custom stored procedure (by copying and modifying the one used by the API to create audiences) to create the audience with the GUID I needed. Look at the ORGLE procs inside the Profile DB to see how this is done.
Anyways, once I created the audiences with the proc. I used central admin to configure the rules.
In general, messing with SharePoint databases is not recommended. Be careful in doing this. Its always better to use the API. That being said, this worked out great. No issues. Well tested.
Code:
--Created by James Hodges 10-19-2012
/****** Object: StoredProcedure [dbo].[usp_AddAudience_Custom] Script Date: 10/19/2012 08:52:19 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp_AddAudience_Custom]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp_AddAudience_Custom]
GO
/****** Object: StoredProcedure [dbo].[usp_AddAudience_Custom] Script Date: 10/19/2012 08:52:19 ******/
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[usp_AddAudience_Custom]
@AudienceID uniqueidentifier,
@OrgleName nvarchar(500),
@OrgleDescription nvarchar(1500) = NULL
AS
DECLARE
@correlationId uniqueidentifier,
@OrgleID uniqueidentifier,
@Error int,
@srError int,
@GroupType smallint,
@bRemove bit,
@OwnerAccountName nvarchar(400),
@partitionID uniqueidentifier
SET NOCOUNT ON
SET @correlationId = NEWID()
SET @GroupType = 2
SET @bRemove = 0
SET @OwnerAccountName = NULL
SET @partitionID = (SELECT TOP 1 PartitionID FROM Orgle_List WITH(NOLOCK) WHERE OrgleID = '00000000-0000-0000-0000-000000000000') --JRH ..This worked for my company. If you have more than one partition you may need to edit this.
SELECT @Error = 1
--check the OrgleID: Note: we make the Audience Name unique across the board.
SELECT @OrgleID = OrgleID FROM Orgle_List WHERE OrgleName = @OrgleName AND PartitionID = @partitionID
IF @bRemove = 1
BEGIN
IF @OrgleID IS NOT NULL
BEGIN
EXEC @Error = Orgle_RemoveOrgle @partitionID, @OrgleID, @correlationId=@correlationId
END
END
ELSE -- add a new one
BEGIN
SELECT @Error = 3
IF @OrgleID IS NOT NULL -- can't create a duplicate orgle name
GOTO CODE_EXIT
SET @Error = 0
BEGIN
-- create the new orgle name
SELECT @OrgleID = @AudienceID
BEGIN TRANSACTION
BEGIN
INSERT INTO Orgle_List ( PartitionID, OrgleID, OrgleName, OrgleNameDescription, OwnerAccountName )
VALUES ( @partitionID, @OrgleID, @OrgleName, @OrgleDescription, @OwnerAccountName )
IF @@ROWCOUNT = 1
BEGIN
-- insert into stats
DECLARE @UpdateTime as DATETIME
SET @UpdateTime = GETUTCDATE()
-- insert into stats
INSERT INTO Orgle_Stats
(PartitionID, OrgleID, GroupType, CreateTime, LastPropertyUpdate, QueryCOunt, GroupCount, MembershipCount, OrgleLock)
VALUES (@partitionID, @OrgleID, @GroupType, @UpdateTime, @UpdateTime, 0, 0, 0, 0)
IF @@ROWCOUNT <> 1
SET @Error = 3
END
ELSE
SET @Error = 3
END
IF @Error = 0
COMMIT TRANSACTION
ELSE
ROLLBACK TRANSACTION
END
END
CODE_EXIT:
SELECT @Error AS ERROR, @OrgleID AS OrgleID
SET NOCOUNT OFF
GO
You can specify the audience GUID in audience rule ..
$newrule = New-Object Microsoft.Office.Server.Audience.AudienceRuleComponent($property,"Not Contains","GUID");
[Void]$newaud.AudienceRules.Add($newrule);