Saturday, August 6, 2011

Sharepoint 2010 Claims site - Add FBA users to Sharepoint group programmatically

Adding FBA users to Sharepoint group programmatically is simple. The hunch is the format "i:0#.f|fbamembershipprovider|" + username

In the format, 
i  - stands for Identity provider 
f  - stands for fba authentication
fbamembershipprovider  - is my providername

Here you go, a simple sample that servers the pupose.

SPSite sitename = new SPSite("SiteURL");
        SPWeb spWeb = sitename.OpenWeb();
        
        String strusername= "i:0#.f|fbamembershipprovider|" + UsernameTextbox.Text;
        spWeb.AllUsers.Add[strusername, EmailTextbox.Text, UsernameTextbox.Text, strusername];
       
         
            if (spUser != null)
            {
                SPGroup spGroup = spWeb.Groups["GROUP_NAME"];
            if (spGroup != null)
            spGroup.AddUser(spUser);
            }


Happy Coding :)

Value does not fall within the expected range - Edit aspx in MOSS 2007

When you edit an aspx page in MOSS 2007 you might get "Value does not fall within the expected range". One of the reason for this issue is the incorrect top-level site URL of the publishing page. If you download the page & open it in notepad, you can find the below line consisting of incorrect top level site URL.
Just update it and upload the page again, your issue should be resolved.

<mso:PublishingPageLayout msdt:dt="string">http://incorrect_servername/_catalogs/masterpage/PageLayout.aspx, Article page</mso:PublishingPageLayout>


If you have many pages with the sample problem, manually updating each & every page could be tedious. In such case you can execute the sample, which will save your time.



const string MASTER_PAGE_LIB = "_catalogs/masterpage";

            string SiteCollectionUrl = "http://siteURL/";

            using (SPSite site = new SPSite(SiteCollectionUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {

                    try
                    {

                        if (PublishingWeb.IsPublishingWeb(web))
                        {
                            PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);


                            foreach (PublishingPage page in pubWeb.GetPublishingPages())
                            {

                                string pageLayout = page.ListItem.Properties["PublishingPageLayout"] as string;


                                if (pageLayout != null)
                                    pageLayout = pageLayout.ToLower();

                                if (String.IsNullOrEmpty(pageLayout))
                                {
                                    Console.WriteLine("Error: Page \"{0}\" does not have a Page Layout assigned.\n", page.Uri);
                                }
                                else if (!pageLayout.Contains(MASTER_PAGE_LIB))
                                {
                                    Console.WriteLine("Error: The Page Layout {0} for Page \"{1}\" does not point to the masterpage document library.\n",
                                        pageLayout, page.Uri);
                                }
                                else if (!pageLayout.StartsWith(SiteCollectionUrl) && pageLayout.StartsWith("http"))
                                {
                                    // here we have a page which has a page layout that has a different URL which we have to fix
                                    Console.WriteLine("Page {0} has incorrect PageLayout Url", page.Uri);
                                    Console.WriteLine("Old URL: {0}", pageLayout);
                                    string pageLayoutWithoutPrefix = pageLayout.Substring(pageLayout.IndexOf(MASTER_PAGE_LIB));
                                    string newPageLayout = SiteCollectionUrl + pageLayoutWithoutPrefix;

                                    try
                                    {
                                        int version = page.ListItem.File.MinorVersion;
                                        page.CheckOut();
                                        page.ListItem.Properties["PublishingPageLayout"] = newPageLayout;
                                        page.ListItem.File.Properties["PublishingPageLayout"] = newPageLayout;
                                        page.ListItem.Update();
                                        page.CheckIn("PublishingPageLayout corrected");

                                        //major version means that the item was published. Let's publish it again.
                                       if (version == 0)
                                            page.ListItem.File.Publish("PublishingPageLayout corrected");

                                        Console.WriteLine("Fixed URL: {0}\n", newPageLayout);
                                    }
                                    catch (Exception e)
                                    {
                                        Console.WriteLine("An error occurred while trying to fix the URL to the page layout:\n{0}", e);
                                    }
                                }
                            }
                        }//if Publishing web
                    }
                }
            }


There is a KB available explicitly for this issue. http://support.microsoft.com/kb/953445
The above sample is based on the KB only.

Hope it helps :)

How to unlock the locked tasks in Sharepoint Workflow

Sometimes in Sharepoint Workflow, tasks will get locked and you will not be able to proceed further.

These locks are placed to prevent the task being updated simultaneously. The workflow runtime locks tasks by setting a field (WorkflowVersion)and persisting that to the database.

if workflowversion is 1 then the tasks are "not locked". Any value apart from "1" symbolises that these tasks are "locked".

Ideally to remove the locks, we need to update WorkflowVersion of the task to "1".

Here you go, the sample for it.


public static void UnlockWorkflowTasks(string siteUrl, string webUrl, string listName)
        {
            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb(webUrl))
                {
                    SPList list = web.Lists[listName];
                    SPListItemCollection items = list.Items;

                    foreach (SPListItem item in items)
                    {
                        SPWorkflowCollection workflows = item.Workflows;
                        foreach (SPWorkflow workflow in workflows)
                        {
                            SPWorkflowTaskCollection tasks = workflow.Tasks;
                            foreach (SPWorkflowTask task in tasks)
                            {
                                if (task[SPBuiltInFieldId.WorkflowVersion].ToString() != "1")
                                {
                                    task[SPBuiltInFieldId.WorkflowVersion] = 1;
                                    task.SystemUpdate();
                                }
                            }
                        }
                    }
                }
            }
        }


Happy Coding :)