Mattersphere Developers Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Go down
avatar
Admin
Admin
Posts : 122
Join date : 2018-12-17
https://mattersphere-devs.forumotion.com

Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection' Empty Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection'

Wed 19 Dec 2018 - 16:23
Original post by Jamie Francis: jamie.francis@emwllp.com


Hi

I'm trying to create a method against a SearchList button in order to copy a hyperlink to the document(s). I've successfully had it working with one document but have now tried to make it work when using multiple-select. I get the above error about converting strings to KVC. It happens on the line that calls the copyAsLink(kv) method. I'm not sure why, perhaps I can't pass a KVC to a method within another script? 'copyAsLink' is part of a custom system script. Any help appreciated!

Here is the code, I've also attached it because sometimes the formatting gets changed when posting code here.

Searchlist method for button:

Code:
FWBS.OMS.UI.Windows.ucSearchControl search = SearchControl as FWBS.OMS.UI.Windows.ucSearchControl;
FWBS.Common.KeyValueCollection kv = new FWBS.Common.KeyValueCollection();

foreach (FWBS.Common.KeyValueCollection item in search.SelectedItems) {
    kv.Add("verLabel", SearchControl.CurrentItem()["verLabel"].Value);
    kv.Add("docid", SearchControl.CurrentItem()["docid"].Value);
    kv.Add("docdesc", SearchControl.CurrentItem()["docdesc"].Value);
}
copyAsLink(kv);

// This is the code for the 'copyAsLink' method referenced above:

public void copyAsLink(KeyValueCollection kv) {

    Clipboard.Clear();
    //Example document links
    //omsmc:opendocument%20629602.1
    //omsmc:opendocument 629602.1

    String html =
    @"Version:0.9
    StartHTML:<<<<<<<1
    EndHTML:<<<<<<<2
    StartFragment:<<<<<<<3
    EndFragment:<<<<<<<4
    StartSelection:<<<<<<<3
    EndSelection:<<<<<<<4
    <!DOCTYPE>
    <HTML>
    <BODY>
    <!--StartFragment -->
    <a href='{0}'>{1}</a>
    <!--EndFragment -->
    </BODY>
    </HTML>";

    string clip = "";

    foreach (FWBS.Common.KeyValueCollection kvc in kv)
    {
        string tempString = "omsmc:opendocument " + kvc["docid"].Value.ToString() + "." + kvc["verLabel"].Value.ToString();
        string tempString2 = "Open document: '" + kvc["docdesc"].Value.ToString() + "'";
        string link2 = String.Format(html, tempString, tempString2);
        clip += link2 + Environment.NewLine;
    }
    Clipboard.SetText(clip, TextDataFormat.Html);
}
avatar
Admin
Admin
Posts : 122
Join date : 2018-12-17
https://mattersphere-devs.forumotion.com

Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection' Empty Re: Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection'

Wed 19 Dec 2018 - 16:25
Original post by Darren Baldwin: darren.baldwin@thomsonreuters.com

Hi Jamie it looks like you are doing a foreach on collections it should be individual items in the collection kv
avatar
Admin
Admin
Posts : 122
Join date : 2018-12-17
https://mattersphere-devs.forumotion.com

Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection' Empty Re: Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection'

Wed 19 Dec 2018 - 16:26
Original post by Jamie Francis: jamie.francis@emwllp.com

Hi Darren

I'm not sure what you mean, can you point out which foreach you're referring to? My idea is that the string called "clip" will eventually be populated with a link to each docid seperated by a new line by looping through the KVC called 'kv'
avatar
Admin
Admin
Posts : 122
Join date : 2018-12-17
https://mattersphere-devs.forumotion.com

Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection' Empty Re: Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection'

Wed 19 Dec 2018 - 16:27
Original post by Darren Baldwin: darren.baldwin@thomsonreuters.com

this is the problem line foreach (FWBS.Common.KeyValueCollection kvc in kv)

what you need to be doing is foreach (KeyValueItem i in kv) then refer to i["docid"].Value;
avatar
Admin
Admin
Posts : 122
Join date : 2018-12-17
https://mattersphere-devs.forumotion.com

Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection' Empty Re: Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection'

Wed 19 Dec 2018 - 16:29
Original post by Jamie Francis: jamie.francis@emwllp.com

I've just tried that, so the code is now:

Code:
foreach (KeyValueItem i in kv)
{
    string tempString = "omsmc:opendocument " + i["docid"].Value + "." + i["verLabel"].Value;
    string tempString2 = "Open document: '" + i["docdesc"].Value + "'";
    string link2 = String.Format(html, tempString, tempString2);
    clip += link2 + Environment.NewLine;
}

However I get an error "Cannot apply indexing with [] to an expression of type 'FWBS.Common.KeyValueItem". Any ideas?
avatar
Admin
Admin
Posts : 122
Join date : 2018-12-17
https://mattersphere-devs.forumotion.com

Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection' Empty Re: Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection'

Wed 19 Dec 2018 - 16:30
Original post by Darren Baldwin: darren.baldwin@thomsonreuters.com

Sory I've misled you a bit. If you have a collection you don't need to do a foreach to reference it by index [""] or if you want to iterate around the keys in the collection it would be i.Value So in your original code try getting rid of the foreach and reference the collection directly so kv["docid"].Value
avatar
Admin
Admin
Posts : 122
Join date : 2018-12-17
https://mattersphere-devs.forumotion.com

Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection' Empty Re: Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection'

Wed 19 Dec 2018 - 16:31
Original post by Jamie Francis: jamie.francis@emwllp.com

Still having trouble with this but i've been working on some other things. I'll come back to it at a later date and either try what you've said or use another method! Thanks though.
Sponsored content

Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection' Empty Re: Unable to cast object 'System.String' to type 'FWBS.Common.KeyValueCollection'

Back to top
Permissions in this forum:
You cannot reply to topics in this forum